MetadataLoadContext.CoreAssembly Property
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets or sets the assembly that denotes the "system assembly" that houses the well-known types such as Int32.
public:
property System::Reflection::Assembly ^ CoreAssembly { System::Reflection::Assembly ^ get(); };
public System.Reflection.Assembly? CoreAssembly { get; }
member this.CoreAssembly : System.Reflection.Assembly
Public ReadOnly Property CoreAssembly As Assembly
Property Value
An Assembly instance.
Remarks
The core assembly is treated differently than other assemblies because references to these well-known types do not include the assembly reference, unlike normal types.
Typically, this assembly is named "mscorlib" or "netstandard". If the core assembly cannot be found, the value will be null
, and many other reflection methods, including those that parse method signatures, will throw an exception.
The CoreAssembly
is determined by passing the coreAssemblyName
parameter passed to the MetadataAssemblyResolver constructor
to the MetadataAssemblyResolver.Resolve method.
If no coreAssemblyName
argument was specified in the constructor of MetadataLoadContext, then default values are used, including "mscorlib", "System.Runtime" and "netstandard".
The designated core assembly does not need to contain the core types directly. It can type forward them to other assemblies. Thus, it is perfectly permissible to use the mscorlib facade as the designated core assembly.
Note that System.Runtime is not an ideal core assembly because it excludes some of the interop-related pseudo-custom attribute types such as DllImportAttribute. However, it can serve if you have no interest in those attributes. The CustomAttributes API will skip those attributes if the core assembly does not include the necessary types.
The core assembly is not loaded until necessary. The following APIs do not trigger the search for the core assembly:
- MetadataLoadContext.LoadFromStream
- MetadataLoadContext.LoadFromAssemblyPath
- MetadataLoadContext.LoadFromByteArray
- Assembly.GetName
- Assembly.FullName
- Assembly.GetReferencedAssemblies
- Assembly.GetTypes
- Assembly.DefinedTypes
- Assembly.GetExportedTypes
- Assembly.GetForwardedTypes
- Assembly.GetType(String, Boolean, Boolean)
- Type.Name
- Type.FullName
- Type.AssemblyQualifiedName
If a core assembly cannot be found or if the core assembly is missing types, this will affect the behavior of the MetadataLoadContext as follows:
APIs that need to parse signatures or typespecs and return the results as Type objects will throw an exception. For example:
APIs that need to compare types to well-known core types will not throw an exception, and the comparison will evaluate to
false
. For example, if you do not specify a core assembly, IsPrimitive will returnfalse
for everything, even types named Int32. Similarly, Type.GetTypeCode will return Object for everything.If a metadata entity sets flags that surface as a pseudo-custom attribute, and the core assembly does not contain the pseudo-custom attribute type, the necessary constructor or any of the parameter types of the constructor, the MetadataLoadContext will not throw. It will omit the pseudo-custom attribute from the list of returned attributes.