Delen via


CSharpCompilation 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 CSharpCompilation sealed : Microsoft::CodeAnalysis::Compilation
public sealed class CSharpCompilation : Microsoft.CodeAnalysis.Compilation
type CSharpCompilation = class
    inherit Compilation
Public NotInheritable Class CSharpCompilation
Inherits Compilation
Inheritance
CSharpCompilation

Fields

_features (Inherited from Compilation)

Properties

Assembly

The IAssemblySymbol that represents the assembly being created.

(Inherited from Compilation)
AssemblyName

Simple assembly name, or null if not specified.

(Inherited from Compilation)
CommonAssembly (Inherited from Compilation)
CommonDynamicType (Inherited from Compilation)
CommonGlobalNamespace (Inherited from Compilation)
CommonObjectType (Inherited from Compilation)
CommonOptions (Inherited from Compilation)
CommonScriptClass (Inherited from Compilation)
CommonScriptGlobalsType (Inherited from Compilation)
CommonSourceModule (Inherited from Compilation)
CommonSyntaxTrees (Inherited from Compilation)
DirectiveReferences
DynamicType

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

(Inherited from Compilation)
ExternalReferences

Metadata references passed to the compilation constructor.

(Inherited from Compilation)
GlobalNamespace

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

(Inherited from Compilation)
IsCaseSensitive
Language
LanguageVersion

The language version that was used to parse the syntax trees of this compilation.

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.

(Inherited from Compilation)
Options

The options the compilation was created with.

ReferencedAssemblyNames
References

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

(Inherited from Compilation)
ScriptClass

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

(Inherited from Compilation)
ScriptCompilationInfo
SourceModule

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

(Inherited from Compilation)
SyntaxTrees

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) (Inherited from Compilation)
ClassifyCommonConversion(ITypeSymbol, ITypeSymbol)

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

ClassifyConversion(ITypeSymbol, ITypeSymbol)

Classifies a conversion from source to destination.

Clone()

Create a duplicate of this compilation with different symbol instances.

CommonAddSyntaxTrees(IEnumerable<SyntaxTree>) (Inherited from Compilation)
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.

(Inherited from Compilation)
CommonClone() (Inherited from Compilation)
CommonContainsSyntaxTree(SyntaxTree) (Inherited from Compilation)
CommonCreateAnonymousTypeSymbol(ImmutableArray<ITypeSymbol>, ImmutableArray<String>, ImmutableArray<Location>, ImmutableArray<Boolean>, ImmutableArray<NullableAnnotation>) (Inherited from Compilation)
CommonCreateAnonymousTypeSymbol(ImmutableArray<ITypeSymbol>, ImmutableArray<String>, ImmutableArray<Location>, ImmutableArray<Boolean>) (Inherited from Compilation)
CommonCreateArrayTypeSymbol(ITypeSymbol, Int32, NullableAnnotation) (Inherited from Compilation)
CommonCreateArrayTypeSymbol(ITypeSymbol, Int32) (Inherited from Compilation)
CommonCreateBuiltinOperator(String, ITypeSymbol, ITypeSymbol, ITypeSymbol) (Inherited from Compilation)
CommonCreateBuiltinOperator(String, ITypeSymbol, ITypeSymbol) (Inherited from Compilation)
CommonCreateErrorNamespaceSymbol(INamespaceSymbol, String) (Inherited from Compilation)
CommonCreateErrorTypeSymbol(INamespaceOrTypeSymbol, String, Int32) (Inherited from Compilation)
CommonCreateFunctionPointerTypeSymbol(ITypeSymbol, RefKind, ImmutableArray<ITypeSymbol>, ImmutableArray<RefKind>, SignatureCallingConvention, ImmutableArray<INamedTypeSymbol>) (Inherited from Compilation)
CommonCreateFunctionPointerTypeSymbol(ITypeSymbol, RefKind, ImmutableArray<ITypeSymbol>, ImmutableArray<RefKind>) (Inherited from Compilation)
CommonCreateNativeIntegerTypeSymbol(Boolean) (Inherited from Compilation)
CommonCreatePointerTypeSymbol(ITypeSymbol) (Inherited from Compilation)
CommonCreateTupleTypeSymbol(ImmutableArray<ITypeSymbol>, ImmutableArray<String>, ImmutableArray<Location>, ImmutableArray<NullableAnnotation>) (Inherited from Compilation)
CommonCreateTupleTypeSymbol(ImmutableArray<ITypeSymbol>, ImmutableArray<String>, ImmutableArray<Location>) (Inherited from Compilation)
CommonCreateTupleTypeSymbol(INamedTypeSymbol, ImmutableArray<String>, ImmutableArray<Location>, ImmutableArray<NullableAnnotation>) (Inherited from Compilation)
CommonCreateTupleTypeSymbol(INamedTypeSymbol, ImmutableArray<String>, ImmutableArray<Location>) (Inherited from Compilation)
CommonGetAssemblyOrModuleSymbol(MetadataReference) (Inherited from Compilation)
CommonGetCompilationNamespace(INamespaceSymbol) (Inherited from Compilation)
CommonGetEntryPoint(CancellationToken) (Inherited from Compilation)
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).

(Inherited from Compilation)
CommonGetSemanticModel(SyntaxTree, SemanticModelOptions)

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,Microsoft.CodeAnalysis.SemanticModelOptions) to get a semantic model. Otherwise, it creates a new semantic model using Microsoft.CodeAnalysis.Compilation.CreateSemanticModel(Microsoft.CodeAnalysis.SyntaxTree,Microsoft.CodeAnalysis.SemanticModelOptions).

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

Creates a new compilation with the specified references.

(Inherited from Compilation)
CommonWithScriptCompilationInfo(ScriptCompilationInfo) (Inherited from Compilation)
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 will be faster than ContainsSymbolsWithName(Func<String,Boolean>, SymbolFilter, CancellationToken) when predicate is just a simple string check.

ContainsSyntaxTree(SyntaxTree)

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

Create(String, IEnumerable<SyntaxTree>, IEnumerable<MetadataReference>, CSharpCompilationOptions)

Creates a new compilation from scratch. Methods such as AddSyntaxTrees or AddReferences on the returned object will allow to continue building up the Compilation incrementally.

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.

(Inherited from Compilation)
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.

(Inherited from Compilation)
CreateArrayTypeSymbol(ITypeSymbol, Int32, NullableAnnotation)

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

(Inherited from Compilation)
CreateArrayTypeSymbol(ITypeSymbol, Int32)

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

(Inherited from 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.

(Inherited from Compilation)
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.

(Inherited from Compilation)
CreateDefaultWin32Resources(Boolean, Boolean, Stream, Stream)

Create a stream filled with default win32 resources.

(Inherited from Compilation)
CreateErrorNamespaceSymbol(INamespaceSymbol, String)

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

(Inherited from Compilation)
CreateErrorTypeSymbol(INamespaceOrTypeSymbol, String, Int32)

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

(Inherited from Compilation)
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.

(Inherited from Compilation)
CreateFunctionPointerTypeSymbol(ITypeSymbol, RefKind, ImmutableArray<ITypeSymbol>, ImmutableArray<RefKind>)

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

(Inherited from Compilation)
CreateNativeIntegerTypeSymbol(Boolean)

Returns a new INamedTypeSymbol representing a native integer.

(Inherited from Compilation)
CreatePointerTypeSymbol(ITypeSymbol)

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

(Inherited from Compilation)
CreateScriptCompilation(String, SyntaxTree, IEnumerable<MetadataReference>, CSharpCompilationOptions, CSharpCompilation, Type, Type)

Creates a new compilation that can be used in scripting.

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.

(Inherited from Compilation)
CreateTupleTypeSymbol(ImmutableArray<ITypeSymbol>, ImmutableArray<String>, ImmutableArray<Location>)

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

(Inherited from Compilation)
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.

(Inherited from Compilation)
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.

(Inherited from Compilation)
Emit(Stream, Stream, Stream, Stream, IEnumerable<ResourceDescription>, EmitOptions, CancellationToken) (Inherited from Compilation)
Emit(Stream, Stream, Stream, Stream, IEnumerable<ResourceDescription>, EmitOptions, IMethodSymbol, CancellationToken) (Inherited from Compilation)
Emit(Stream, Stream, Stream, Stream, IEnumerable<ResourceDescription>, EmitOptions, IMethodSymbol, Stream, IEnumerable<EmbeddedText>, CancellationToken) (Inherited from Compilation)
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.

(Inherited from Compilation)
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.

(Inherited from Compilation)
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.

(Inherited from Compilation)
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.

(Inherited from Compilation)
GetAssemblyOrModuleSymbol(MetadataReference)

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

(Inherited from Compilation)
GetCompilationNamespace(INamespaceSymbol)

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

(Inherited from Compilation)
GetDeclarationDiagnostics(CancellationToken)

Gets the diagnostics produced during symbol declaration headers. There are no diagnostics for accessor or method bodies, for example.

GetDiagnostics(CancellationToken)

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

GetDirectiveReference(ReferenceDirectiveTriviaSyntax)

Returns a metadata reference that a given #r resolves to.

GetEntryPoint(CancellationToken)

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

(Inherited from Compilation)
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 of a compilation. There are no diagnostics for declarations or accessor or method bodies, for example.

GetSemanticModel(SyntaxTree, Boolean)

Gets a new SyntaxTreeSemanticModel for the specified syntax tree.

GetSemanticModel(SyntaxTree, SemanticModelOptions)

Gets a new SyntaxTreeSemanticModel for the specified syntax tree.

GetSemanticModel(SyntaxTree, SemanticModelOptions) (Inherited from Compilation)
GetSpecialType(SpecialType)

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

(Inherited from 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 will be faster than GetSymbolsWithName(Func<String,Boolean>, SymbolFilter, CancellationToken) when predicate is just a simple string check. name is case sensitive.

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.
(Inherited from Compilation)
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.

(Inherited from Compilation)
GetUnreferencedAssemblyIdentities(Diagnostic)

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

(Inherited from Compilation)
GetUsedAssemblyReferences(CancellationToken)
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.

(Inherited from Compilation)
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.

(Inherited from Compilation)
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.

(Inherited from Compilation)
RemoveAllReferences()

Creates a new compilation without any metadata references

RemoveAllSyntaxTrees()

Creates a new compilation without any syntax trees. Preserves metadata info from this compilation 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 without the old tree but with the new tree.

SupportsRuntimeCapability(RuntimeCapability)

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

(Inherited from Compilation)
ToMetadataReference(ImmutableArray<String>, Boolean)
WithAssemblyName(String)

Creates a new compilation with the specified name.

WithOptions(CompilationOptions)

Creates a new compilation with the specified compilation options.

(Inherited from Compilation)
WithOptions(CSharpCompilationOptions)

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(CSharpScriptCompilationInfo)

Returns a new compilation with the given compilation set as the previous submission.

WithScriptCompilationInfo(ScriptCompilationInfo) (Inherited from Compilation)

Extension Methods

ClassifyConversion(Compilation, ITypeSymbol, ITypeSymbol)
Emit(CSharpCompilation, String, String, String, String, IEnumerable<ResourceDescription>, CancellationToken)

Emit the IL for the compilation into the specified stream.

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