Compilation Class

Definition

The compilation object is an immutable representation of a single invocation of the compiler. Although immutable, a compilation is also on-demand, and will realize and cache data as necessary. A compilation can produce a new compilation from existing compilation with the application of small deltas. In many cases, it is more efficient than creating a new compilation from scratch, as the new compilation can reuse information from the old compilation.

public ref class Compilation abstract
public abstract class Compilation
type Compilation = class
Public MustInherit Class Compilation
Inheritance
Compilation
Derived

Fields

_features

Properties

Assembly

The IAssemblySymbol that represents the assembly being created.

AssemblyName

Simple assembly name, or null if not specified.

CommonAssembly
CommonDynamicType
CommonGlobalNamespace
CommonObjectType
CommonOptions
CommonScriptClass
CommonScriptGlobalsType
CommonSourceModule
CommonSyntaxTrees
DirectiveReferences

Unique metadata references specified via #r directive in the source code of this compilation.

DynamicType

The TypeSymbol for the type 'dynamic' in this Compilation.

ExternalReferences

Metadata references passed to the compilation constructor.

GlobalNamespace

The root namespace that contains all namespaces and types defined in source code or in referenced metadata, merged into a single namespace hierarchy.

IsCaseSensitive

Returns true if this is a case sensitive compilation, false otherwise. Case sensitivity affects compilation features such as name lookup as well as choosing what names to emit when there are multiple different choices (for example between a virtual method and an override).

Language

Gets the source language ("C#" or "Visual Basic").

ObjectType

The INamedTypeSymbol for the .NET System.Object type, which could have a TypeKind of Error if there was no COR Library in this Compilation.

Options

Gets the options the compilation was created with.

ReferencedAssemblyNames

Assembly identities of all assemblies directly referenced by this compilation.

References

All metadata references -- references passed to the compilation constructor as well as references specified via #r directives.

ScriptClass

A symbol representing the implicit Script class. This is null if the class is not defined in the compilation.

ScriptCompilationInfo
SourceModule

Gets the IModuleSymbol for the module being created by compiling all of the source code.

SyntaxTrees

Gets the syntax trees (parsed from source code) that this compilation was created with.

Methods

AddReferences(IEnumerable<MetadataReference>)

Creates a new compilation with additional metadata references.

AddReferences(MetadataReference[])

Creates a new compilation with additional metadata references.

AddSyntaxTrees(IEnumerable<SyntaxTree>)

Creates a new compilation with additional syntax trees.

AddSyntaxTrees(SyntaxTree[])

Creates a new compilation with additional syntax trees.

AppendDefaultVersionResource(Stream)
CheckTupleElementLocations(Int32, ImmutableArray<Location>)
CheckTupleElementNames(Int32, ImmutableArray<String>)

Check that if any names are provided, and their number matches the expected cardinality. Returns a normalized version of the element names (empty array if all the names are null).

CheckTupleElementNullableAnnotations(Int32, ImmutableArray<NullableAnnotation>)
ClassifyCommonConversion(ITypeSymbol, ITypeSymbol)

Classifies a conversion from source to destination according to this compilation's programming language.

Clone()

Creates a new compilation equivalent to this one with different symbol instances.

CommonAddSyntaxTrees(IEnumerable<SyntaxTree>)
CommonBindScriptClass()

Resolves a symbol that represents script container (Script class). Uses the full name of the container class stored in ScriptClassName to find the symbol.

CommonClone()
CommonContainsSyntaxTree(SyntaxTree)
CommonCreateAnonymousTypeSymbol(ImmutableArray<ITypeSymbol>, ImmutableArray<String>, ImmutableArray<Location>, ImmutableArray<Boolean>, ImmutableArray<NullableAnnotation>)
CommonCreateAnonymousTypeSymbol(ImmutableArray<ITypeSymbol>, ImmutableArray<String>, ImmutableArray<Location>, ImmutableArray<Boolean>)
CommonCreateArrayTypeSymbol(ITypeSymbol, Int32, NullableAnnotation)
CommonCreateArrayTypeSymbol(ITypeSymbol, Int32)
CommonCreateBuiltinOperator(String, ITypeSymbol, ITypeSymbol, ITypeSymbol)
CommonCreateBuiltinOperator(String, ITypeSymbol, ITypeSymbol)
CommonCreateErrorNamespaceSymbol(INamespaceSymbol, String)
CommonCreateErrorTypeSymbol(INamespaceOrTypeSymbol, String, Int32)
CommonCreateFunctionPointerTypeSymbol(ITypeSymbol, RefKind, ImmutableArray<ITypeSymbol>, ImmutableArray<RefKind>, SignatureCallingConvention, ImmutableArray<INamedTypeSymbol>)
CommonCreateFunctionPointerTypeSymbol(ITypeSymbol, RefKind, ImmutableArray<ITypeSymbol>, ImmutableArray<RefKind>)
CommonCreateNativeIntegerTypeSymbol(Boolean)
CommonCreatePointerTypeSymbol(ITypeSymbol)
CommonCreateTupleTypeSymbol(ImmutableArray<ITypeSymbol>, ImmutableArray<String>, ImmutableArray<Location>, ImmutableArray<NullableAnnotation>)
CommonCreateTupleTypeSymbol(ImmutableArray<ITypeSymbol>, ImmutableArray<String>, ImmutableArray<Location>)
CommonCreateTupleTypeSymbol(INamedTypeSymbol, ImmutableArray<String>, ImmutableArray<Location>, ImmutableArray<NullableAnnotation>)
CommonCreateTupleTypeSymbol(INamedTypeSymbol, ImmutableArray<String>, ImmutableArray<Location>)
CommonGetAssemblyOrModuleSymbol(MetadataReference)
CommonGetCompilationNamespace(INamespaceSymbol)
CommonGetEntryPoint(CancellationToken)
CommonGetSemanticModel(SyntaxTree, Boolean)

Gets a SemanticModel for the given syntaxTree. If Microsoft.CodeAnalysis.Compilation.SemanticModelProvider is non-null, it attempts to use Microsoft.CodeAnalysis.SemanticModelProvider.GetSemanticModel(Microsoft.CodeAnalysis.SyntaxTree,Microsoft.CodeAnalysis.Compilation,System.Boolean) to get a semantic model. Otherwise, it creates a new semantic model using Microsoft.CodeAnalysis.Compilation.CreateSemanticModel(Microsoft.CodeAnalysis.SyntaxTree,System.Boolean).

CommonGetSpecialType(SpecialType)
CommonGetTypeByMetadataName(String)
CommonRemoveAllSyntaxTrees()
CommonRemoveSyntaxTrees(IEnumerable<SyntaxTree>)
CommonReplaceSyntaxTree(SyntaxTree, SyntaxTree)
CommonWithAssemblyName(String)
CommonWithOptions(CompilationOptions)
CommonWithReferences(IEnumerable<MetadataReference>)

Creates a new compilation with the specified references.

CommonWithScriptCompilationInfo(ScriptCompilationInfo)
ContainsSymbolsWithName(Func<String,Boolean>, SymbolFilter, CancellationToken)

Return true if there is a source declaration symbol name that meets given predicate.

ContainsSymbolsWithName(String, SymbolFilter, CancellationToken)

Return true if there is a source declaration symbol name that matches the provided name. This may be faster than ContainsSymbolsWithName(Func<String,Boolean>, SymbolFilter, CancellationToken) when predicate is just a simple string check. name is case sensitive or not depending on the target language.

ContainsSyntaxTree(SyntaxTree)

Returns true if this compilation contains the specified tree. False otherwise.

CreateAnonymousTypeSymbol(ImmutableArray<ITypeSymbol>, ImmutableArray<String>, ImmutableArray<Boolean>, ImmutableArray<Location>, ImmutableArray<NullableAnnotation>)

Returns a new anonymous type symbol with the given member types, names, source locations, and nullable annotations. Anonymous type members will be readonly by default. Writable properties are supported in VB and can be created by passing in false in the appropriate locations in memberIsReadOnly.

CreateAnonymousTypeSymbol(ImmutableArray<ITypeSymbol>, ImmutableArray<String>, ImmutableArray<Boolean>, ImmutableArray<Location>)

Returns a new anonymous type symbol with the given member types, names, and source locations. Anonymous type members will be readonly by default. Writable properties are supported in VB and can be created by passing in false in the appropriate locations in memberIsReadOnly.

CreateArrayTypeSymbol(ITypeSymbol, Int32, NullableAnnotation)

Returns a new ArrayTypeSymbol representing an array type tied to the base types of the COR Library in this Compilation.

CreateArrayTypeSymbol(ITypeSymbol, Int32)

Returns a new ArrayTypeSymbol representing an array type tied to the base types of the COR Library in this Compilation.

CreateBuiltinOperator(String, ITypeSymbol, ITypeSymbol, ITypeSymbol)

Creates an IMethodSymbol whose MethodKind is BuiltinOperator for a binary operator. Built-in operators are commonly created for symbols like bool int.operator ==(int v1, int v2) which the language implicitly supports, even if such a symbol is not explicitly defined for that type in either source or metadata.

CreateBuiltinOperator(String, ITypeSymbol, ITypeSymbol)

Creates an IMethodSymbol whose MethodKind is BuiltinOperator for a unary operator. Built-in operators are commonly created for symbols like bool int.operator -(int value) which the language implicitly supports, even if such a symbol is not explicitly defined for that type in either source or metadata.

CreateDefaultWin32Resources(Boolean, Boolean, Stream, Stream)

Create a stream filled with default win32 resources.

CreateErrorNamespaceSymbol(INamespaceSymbol, String)

Returns a new INamespaceSymbol representing an error (missing) namespace with the given name.

CreateErrorTypeSymbol(INamespaceOrTypeSymbol, String, Int32)

Returns a new INamedTypeSymbol representing an error type with the given name and arity in the given optional container.

CreateFunctionPointerTypeSymbol(ITypeSymbol, RefKind, ImmutableArray<ITypeSymbol>, ImmutableArray<RefKind>, SignatureCallingConvention, ImmutableArray<INamedTypeSymbol>)

Returns a new IFunctionPointerTypeSymbol representing a function pointer type tied to types in this Compilation.

CreateFunctionPointerTypeSymbol(ITypeSymbol, RefKind, ImmutableArray<ITypeSymbol>, ImmutableArray<RefKind>)

Returns a new IFunctionPointerTypeSymbol representing a function pointer type tied to types in this Compilation.

CreateNativeIntegerTypeSymbol(Boolean)

Returns a new INamedTypeSymbol representing a native integer.

CreatePointerTypeSymbol(ITypeSymbol)

Returns a new IPointerTypeSymbol representing a pointer type tied to a type in this Compilation.

CreateTupleTypeSymbol(ImmutableArray<ITypeSymbol>, ImmutableArray<String>, ImmutableArray<Location>, ImmutableArray<NullableAnnotation>)

Returns a new INamedTypeSymbol with the given element types and (optional) element names, locations, and nullable annotations.

CreateTupleTypeSymbol(ImmutableArray<ITypeSymbol>, ImmutableArray<String>, ImmutableArray<Location>)

Returns a new INamedTypeSymbol with the given element types, names, and locations.

CreateTupleTypeSymbol(INamedTypeSymbol, ImmutableArray<String>, ImmutableArray<Location>, ImmutableArray<NullableAnnotation>)

Returns a new INamedTypeSymbol with the given underlying type and (optional) element names, locations, and nullable annotations. The underlying type needs to be tuple-compatible.

CreateTupleTypeSymbol(INamedTypeSymbol, ImmutableArray<String>, ImmutableArray<Location>)

Returns a new INamedTypeSymbol with the given underlying type and element names and locations. The underlying type needs to be tuple-compatible.

Emit(Stream, Stream, Stream, Stream, IEnumerable<ResourceDescription>, EmitOptions, CancellationToken)
Emit(Stream, Stream, Stream, Stream, IEnumerable<ResourceDescription>, EmitOptions, IMethodSymbol, CancellationToken)
Emit(Stream, Stream, Stream, Stream, IEnumerable<ResourceDescription>, EmitOptions, IMethodSymbol, Stream, IEnumerable<EmbeddedText>, CancellationToken)
Emit(Stream, Stream, Stream, Stream, IEnumerable<ResourceDescription>, EmitOptions, IMethodSymbol, Stream, IEnumerable<EmbeddedText>, Stream, CancellationToken)

Emit the IL for the compiled source code into the specified stream.

EmitDifference(EmitBaseline, IEnumerable<SemanticEdit>, Func<ISymbol,Boolean>, Stream, Stream, Stream, CancellationToken)

Emit the differences between the compilation and the previous generation for Edit and Continue. The differences are expressed as added and changed symbols, and are emitted as metadata, IL, and PDB deltas. A representation of the current compilation is returned as an EmitBaseline for use in a subsequent Edit and Continue.

EmitDifference(EmitBaseline, IEnumerable<SemanticEdit>, Func<ISymbol, Boolean>, Stream, Stream, Stream, ICollection<MethodDefinitionHandle>, CancellationToken)
Obsolete.

Emit the differences between the compilation and the previous generation for Edit and Continue. The differences are expressed as added and changed symbols, and are emitted as metadata, IL, and PDB deltas. A representation of the current compilation is returned as an EmitBaseline for use in a subsequent Edit and Continue.

EmitDifference(EmitBaseline, IEnumerable<SemanticEdit>, Stream, Stream, Stream, ICollection<MethodDefinitionHandle>, CancellationToken)
Obsolete.

Emit the differences between the compilation and the previous generation for Edit and Continue. The differences are expressed as added and changed symbols, and are emitted as metadata, IL, and PDB deltas. A representation of the current compilation is returned as an EmitBaseline for use in a subsequent Edit and Continue.

GetAssemblyOrModuleSymbol(MetadataReference)

Gets the IAssemblySymbol or IModuleSymbol for a metadata reference used to create this compilation.

GetCompilationNamespace(INamespaceSymbol)

Gets the corresponding compilation namespace for the specified module or assembly namespace.

GetDeclarationDiagnostics(CancellationToken)

Gets the diagnostics produced during symbol declaration.

GetDiagnostics(CancellationToken)

Gets all the diagnostics for the compilation, including syntax, declaration, and binding. Does not include any diagnostics that might be produced during emit, see EmitResult.

GetEntryPoint(CancellationToken)

Returns the Main method that will serves as the entry point of the assembly, if it is executable (and not a script).

GetMetadataReference(IAssemblySymbol)

Gets the MetadataReference that corresponds to the assembly symbol.

GetMethodBodyDiagnostics(CancellationToken)

Gets the diagnostics produced during the analysis of method bodies and field initializers.

GetParseDiagnostics(CancellationToken)

Gets the diagnostics produced during the parsing stage.

GetRequiredLanguageVersion(Diagnostic)

Returns the required language version found in a Diagnostic, if any is found. Returns null if none is found.

GetSemanticModel(SyntaxTree, Boolean)

Gets a new SemanticModel for the specified syntax tree.

GetSpecialType(SpecialType)

Get the symbol for the predefined type from the Cor Library referenced by this compilation.

GetSymbolsWithName(Func<String,Boolean>, SymbolFilter, CancellationToken)

Return source declaration symbols whose name meets given predicate.

GetSymbolsWithName(String, SymbolFilter, CancellationToken)

Return source declaration symbols whose name matches the provided name. This may be faster than GetSymbolsWithName(Func<String,Boolean>, SymbolFilter, CancellationToken) when predicate is just a simple string check. name is case sensitive or not depending on the target language.

GetTypeByMetadataName(String)

Gets the type within the compilation's assembly and all referenced assemblies (other than those that can only be referenced via an extern alias) using its canonical CLR metadata name. This lookup follows the following order:

  1. If the type is found in the compilation's assembly, that type is returned.
  2. Next, the core library (the library that defines System.Object and has no assembly references) is searched. If the type is found there, that type is returned.
  3. Finally, all remaining referenced non-extern assemblies are searched. If one and only one type matching the provided metadata name is found, that single type is returned. Accessibility is ignored for this check.
GetTypesByMetadataName(String)

Gets all types with the compilation's assembly and all referenced assemblies that have the given canonical CLR metadata name. Accessibility to the current assembly is ignored when searching for matching type names.

GetUnreferencedAssemblyIdentities(Diagnostic)

Given a Diagnostic reporting unreferenced AssemblyIdentitys, returns the actual AssemblyIdentity instances that were not referenced.

GetUsedAssemblyReferences(CancellationToken)

Unique metadata assembly references that are considered to be used by this compilation. For example, if a type declared in a referenced assembly is referenced in source code within this compilation, the reference is considered to be used. Etc. The returned set is a subset of references returned by References API. The result is undefined if the compilation contains errors.

The effect of imported namespaces on result of this API depends on whether reporting of unused imports is disabled for the compilation. The reporting of unused imports is disabled if DocumentationMode is set to None.

When unused imports reporting is disabled, all referenced assemblies containing any types that belong to imported namespaces are included in the result. I.e. considered used.

When unused imports reporting is enabled, imported namespaces do not have effect on the result of this API. Therefore, removing assembly references that aren't in the result, could potentially cause error "CS0246: The type or namespace name could not be found (are you missing a using directive or an assembly reference?)" on an unused namespace import. However, that import would be reported by compiler as unused for the compilation on which this API was invoked. In order to avoid the errors, it is recommended to remove unused assembly references and unused imports at the same time.

HasImplicitConversion(ITypeSymbol, ITypeSymbol)

Returns true if there is an implicit (C#) or widening (VB) conversion from fromType to toType. Returns false if either fromType or toType is null, or if no such conversion exists.

IsSymbolAccessibleWithin(ISymbol, ISymbol, ITypeSymbol)

Checks if symbol is accessible from within within. An optional qualifier of type throughType is used to resolve protected access for instance members. All symbols are required to be from this compilation or some assembly referenced (References) by this compilation. within is required to be an INamedTypeSymbol or IAssemblySymbol.

RemoveAllReferences()

Creates a new compilation without any metadata references.

RemoveAllSyntaxTrees()

Creates a new compilation without any syntax trees. Preserves metadata info for use with trees added later.

RemoveReferences(IEnumerable<MetadataReference>)

Creates a new compilation without the specified metadata references.

RemoveReferences(MetadataReference[])

Creates a new compilation without the specified metadata references.

RemoveSyntaxTrees(IEnumerable<SyntaxTree>)

Creates a new compilation without the specified syntax trees. Preserves metadata info for use with trees added later.

RemoveSyntaxTrees(SyntaxTree[])

Creates a new compilation without the specified syntax trees. Preserves metadata info for use with trees added later.

ReplaceReference(MetadataReference, MetadataReference)

Creates a new compilation with an old metadata reference replaced with a new metadata reference.

ReplaceSyntaxTree(SyntaxTree, SyntaxTree)

Creates a new compilation with an old syntax tree replaced with a new syntax tree. Reuses metadata from old compilation object.

SupportsRuntimeCapability(RuntimeCapability)

Determines whether the runtime this Compilation is targeting supports a particular capability.

SyntaxTreeCommonFeatures(IEnumerable<SyntaxTree>)
ToMetadataReference(ImmutableArray<String>, Boolean)

Creates a metadata reference for this compilation.

WithAssemblyName(String)

Creates a compilation with the specified assembly name.

WithOptions(CompilationOptions)

Creates a new compilation with the specified compilation options.

WithReferences(IEnumerable<MetadataReference>)

Creates a new compilation with the specified references.

WithReferences(MetadataReference[])

Creates a new compilation with the specified references.

WithScriptCompilationInfo(ScriptCompilationInfo)

Extension Methods

ClassifyConversion(Compilation, ITypeSymbol, ITypeSymbol)
WithAnalyzers(Compilation, ImmutableArray<DiagnosticAnalyzer>, AnalyzerOptions, CancellationToken)
Obsolete.

Returns a new compilation with attached diagnostic analyzers.

WithAnalyzers(Compilation, ImmutableArray<DiagnosticAnalyzer>, AnalyzerOptions)

Returns a new compilation with attached diagnostic analyzers.

WithAnalyzers(Compilation, ImmutableArray<DiagnosticAnalyzer>, CompilationWithAnalyzersOptions)

Returns a new compilation with attached diagnostic analyzers.

Emit(Compilation, String, String, String, String, IEnumerable<ResourceDescription>, CancellationToken)

Emit the IL for the compilation into the specified stream.

AliasImports(Compilation)

Gets the compilation AliasImports property.

ClassifyConversion(Compilation, ITypeSymbol, ITypeSymbol)

Determines what kind of conversion there is between the specified types.

GetSpecialType(Compilation, SpecialType)

Gets the special type symbol in current compilation.

MemberImports(Compilation)

Gets the compilation MemberImports property.

RootNamespace(Compilation)

Gets the compilation RootNamespace property.

Applies to