Assembly Class

Definition

Represents an assembly, which is a reusable, versionable, and self-describing building block of a common language runtime application.

public abstract class Assembly
public abstract class Assembly : System.Reflection.ICustomAttributeProvider, System.Runtime.Serialization.ISerializable
public abstract class Assembly : System.Reflection.ICustomAttributeProvider
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Serializable]
public class Assembly : System.Reflection.ICustomAttributeProvider, System.Runtime.InteropServices._Assembly, System.Runtime.Serialization.ISerializable, System.Security.IEvidenceFactory
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class Assembly : System.Reflection.ICustomAttributeProvider, System.Runtime.InteropServices._Assembly, System.Runtime.Serialization.ISerializable, System.Security.IEvidenceFactory
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class Assembly : System.Reflection.ICustomAttributeProvider, System.Runtime.InteropServices._Assembly, System.Runtime.Serialization.ISerializable, System.Security.IEvidenceFactory
Inheritance
Assembly
Derived
Attributes
Implements

Examples

The following code example shows how to obtain the currently executing assembly, create an instance of a type contained in that assembly, and invoke one of the type's methods with late binding. For this purpose, the code example defines a class named Example, with a method named SampleMethod. The constructor of the class accepts an integer, which is used to compute the return value of the method.

The code example also demonstrates the use of the GetName method to obtain an AssemblyName object that can be used to parse the full name of the assembly. The example displays the version number of the assembly, the CodeBase property, and the EntryPoint property.

using System;
using System.Reflection;
using System.Security.Permissions;

[assembly:AssemblyVersionAttribute("1.0.2000.0")]

public class Example
{
    private int factor;
    public Example(int f)
    {
        factor = f;
    }

    public int SampleMethod(int x)
    {
        Console.WriteLine("\nExample.SampleMethod({0}) executes.", x);
        return x * factor;
    }

    public static void Main()
    {
        Assembly assem = typeof(Example).Assembly;

        Console.WriteLine("Assembly Full Name:");
        Console.WriteLine(assem.FullName);

        // The AssemblyName type can be used to parse the full name.
        AssemblyName assemName = assem.GetName();
        Console.WriteLine("\nName: {0}", assemName.Name);
        Console.WriteLine("Version: {0}.{1}",
            assemName.Version.Major, assemName.Version.Minor);

        Console.WriteLine("\nAssembly CodeBase:");
        Console.WriteLine(assem.CodeBase);

        // Create an object from the assembly, passing in the correct number
        // and type of arguments for the constructor.
        Object o = assem.CreateInstance("Example", false,
            BindingFlags.ExactBinding,
            null, new Object[] { 2 }, null, null);

        // Make a late-bound call to an instance method of the object.
        MethodInfo m = assem.GetType("Example").GetMethod("SampleMethod");
        Object ret = m.Invoke(o, new Object[] { 42 });
        Console.WriteLine("SampleMethod returned {0}.", ret);

        Console.WriteLine("\nAssembly entry point:");
        Console.WriteLine(assem.EntryPoint);
    }
}

/* This code example produces output similar to the following:

Assembly Full Name:
source, Version=1.0.2000.0, Culture=neutral, PublicKeyToken=null

Name: source
Version: 1.0

Assembly CodeBase:
file:///C:/sdtree/AssemblyClass/cs/source.exe

Example.SampleMethod(42) executes.
SampleMethod returned 84.

Assembly entry point:
Void Main()
 */

Remarks

Use the Assembly class to load assemblies, to explore the metadata and constituent parts of assemblies, to discover the types contained in assemblies, and to create instances of those types.

To get an array of Assembly objects representing the assemblies currently loaded into an application domain (for example, the default application domain of a simple project), use the AppDomain.GetAssemblies method.

To load assemblies dynamically, the Assembly class provides the following static methods (Shared methods in Visual Basic). Assemblies are loaded into the application domain where the load operation occurs.

  • The recommended way to load assemblies is to use the Load method, which identifies the assembly to be loaded by its display name (for example, "System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"). The search for the assembly follows the rules described in How the Runtime Locates Assemblies.

  • The ReflectionOnlyLoad and ReflectionOnlyLoadFrom methods enable you to load an assembly for reflection, but not for execution. For example, an assembly that targets a 64-bit platform can be examined by code that is running on a 32-bit platform.

  • The LoadFile and LoadFrom methods are provided for rare scenarios in which an assembly must be identified by path.

To get an Assembly object for the currently executing assembly, use the GetExecutingAssembly method.

Many members of the Assembly class provide information about an assembly. For example:

  • The GetName method returns an AssemblyName object that provides access to the parts of the assembly display name.

  • The GetCustomAttributes method lists the attributes applied to the assembly.

  • The GetFiles method provides access to the files in the assembly manifest.

  • The GetManifestResourceNames method provides the names of the resources in the assembly manifest.

The GetTypes method lists all the types in the assembly. The GetExportedTypes method lists the types that are visible to callers outside the assembly. The GetType method can be used to search for a particular type in the assembly. The CreateInstance method can be used to search for and create instances of types in the assembly.

For more information on assemblies, see the "Application Domains and Assemblies" section in the Application Domains topic.

Constructors

Assembly()

Initializes a new instance of the Assembly class.

Properties

CodeBase
Obsolete.
Obsolete.

Gets the location of the assembly as specified originally, for example, in an AssemblyName object.

CustomAttributes

Gets a collection that contains this assembly's custom attributes.

DefinedTypes

Gets a collection of the types defined in this assembly.

EntryPoint

Gets the entry point of this assembly.

EscapedCodeBase
Obsolete.
Obsolete.

Gets the URI, including escape characters, that represents the codebase.

Evidence

Gets the evidence for this assembly.

ExportedTypes

Gets a collection of the public types defined in this assembly that are visible outside the assembly.

FullName

Gets the display name of the assembly.

GlobalAssemblyCache
Obsolete.

Gets a value indicating whether the assembly was loaded from the global assembly cache (.NET Framework only).

HostContext

Gets the host context with which the assembly was loaded.

ImageRuntimeVersion

Gets a string representing the version of the common language runtime (CLR) saved in the file containing the manifest.

IsCollectible

Gets a value that indicates whether this assembly is held in a collectible AssemblyLoadContext.

IsDynamic

Gets a value that indicates whether the current assembly was generated dynamically in the current process by using reflection emit.

IsFullyTrusted

Gets a value that indicates whether the current assembly is loaded with full trust.

Location

Gets the full path or UNC location of the loaded file that contains the manifest.

ManifestModule

Gets the module that contains the manifest for the current assembly.

Modules

Gets a collection that contains the modules in this assembly.

PermissionSet

Gets the grant set of the current assembly.

ReflectionOnly

Gets a Boolean value indicating whether this assembly was loaded into the reflection-only context.

SecurityRuleSet

Gets a value that indicates which set of security rules the common language runtime (CLR) enforces for this assembly.

Methods

CreateInstance(String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[])

Locates the specified type from this assembly and creates an instance of it using the system activator, with optional case-sensitive search and having the specified culture, arguments, and binding and activation attributes.

CreateInstance(String, Boolean)

Locates the specified type from this assembly and creates an instance of it using the system activator, with optional case-sensitive search.

CreateInstance(String)

Locates the specified type from this assembly and creates an instance of it using the system activator, using case-sensitive search.

CreateQualifiedName(String, String)

Creates the name of a type qualified by the display name of its assembly.

Equals(Object)

Determines whether this assembly and the specified object are equal.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetAssembly(Type)

Gets the currently loaded assembly in which the specified type is defined.

GetCallingAssembly()

Returns the Assembly of the method that invoked the currently executing method.

GetCustomAttributes(Boolean)

Gets all the custom attributes for this assembly.

GetCustomAttributes(Type, Boolean)

Gets the custom attributes for this assembly as specified by type.

GetCustomAttributesData()

Returns information about the attributes that have been applied to the current Assembly, expressed as CustomAttributeData objects.

GetEntryAssembly()

Gets the entry assembly for the running application.

GetExecutingAssembly()

Gets the assembly that contains the code that is currently executing.

GetExportedTypes()

Gets the public types defined in this assembly that are visible outside the assembly.

GetFile(String)

Gets a FileStream for the specified file in the file table of the manifest of this assembly.

GetFiles()

Gets the files in the file table of an assembly manifest.

GetFiles(Boolean)

Gets the files in the file table of an assembly manifest, specifying whether to include resource modules.

GetForwardedTypes()
GetHashCode()

Returns the hash code for this instance.

GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetLoadedModules()

Gets all the loaded modules that are part of this assembly.

GetLoadedModules(Boolean)

Gets all the loaded modules that are part of this assembly, specifying whether to include resource modules.

GetManifestResourceInfo(String)

Returns information about how the given resource has been persisted.

GetManifestResourceNames()

Returns the names of all the resources in this assembly.

GetManifestResourceStream(String)

Loads the specified manifest resource from this assembly.

GetManifestResourceStream(Type, String)

Loads the specified manifest resource, scoped by the namespace of the specified type, from this assembly.

GetModule(String)

Gets the specified module in this assembly.

GetModules()

Gets all the modules that are part of this assembly.

GetModules(Boolean)

Gets all the modules that are part of this assembly, specifying whether to include resource modules.

GetName()

Gets an AssemblyName for this assembly.

GetName(Boolean)

Gets an AssemblyName for this assembly, setting the codebase as specified by copiedName.

GetObjectData(SerializationInfo, StreamingContext)
Obsolete.

Gets serialization information with all of the data needed to reinstantiate this assembly.

GetReferencedAssemblies()

Gets the AssemblyName objects for all the assemblies referenced by this assembly.

GetSatelliteAssembly(CultureInfo, Version)

Gets the specified version of the satellite assembly for the specified culture.

GetSatelliteAssembly(CultureInfo)

Gets the satellite assembly for the specified culture.

GetType()
GetType()

Gets the Type of the current instance.

(Inherited from Object)
GetType(String, Boolean, Boolean)

Gets the Type object with the specified name in the assembly instance, with the options of ignoring the case, and of throwing an exception if the type is not found.

GetType(String, Boolean)

Gets the Type object with the specified name in the assembly instance and optionally throws an exception if the type is not found.

GetType(String)

Gets the Type object with the specified name in the assembly instance.

GetTypes()

Gets all types defined in this assembly.

IsDefined(Type, Boolean)

Indicates whether or not a specified attribute has been applied to the assembly.

Load(AssemblyName, Evidence)
Obsolete.

Loads an assembly given its AssemblyName. The assembly is loaded using the supplied evidence.

Load(AssemblyName)

Loads an assembly given its AssemblyName.

Load(Byte[], Byte[], Evidence)
Obsolete.

Loads the assembly with a common object file format (COFF)-based image containing an emitted assembly, optionally including symbols and evidence for the assembly.

Load(Byte[], Byte[], SecurityContextSource)

Loads the assembly with a common object file format (COFF)-based image containing an emitted assembly, optionally including symbols and specifying the source for the security context.

Load(Byte[], Byte[])

Loads the assembly with a common object file format (COFF)-based image containing an emitted assembly, optionally including symbols for the assembly.

Load(Byte[])

Loads the assembly with a common object file format (COFF)-based image containing an emitted assembly.

Load(String, Evidence)
Obsolete.

Loads an assembly given its display name and using the supplied evidence.

Load(String)

Loads an assembly with the specified name.

LoadFile(String, Evidence)
Obsolete.

Loads an assembly given its path, loading the assembly using the supplied evidence.

LoadFile(String)

Loads the contents of an assembly file on the specified path.

LoadFrom(String, Byte[], AssemblyHashAlgorithm)
Obsolete.

Loads an assembly given its file name or path, hash value, and hash algorithm.

LoadFrom(String, Evidence, Byte[], AssemblyHashAlgorithm)
Obsolete.

Loads an assembly given its file name or path, security evidence, hash value, and hash algorithm.

LoadFrom(String, Evidence)
Obsolete.

Loads an assembly given its file name or path and supplying security evidence.

LoadFrom(String)

Loads an assembly given its file name or path.

LoadModule(String, Byte[], Byte[])

Loads the module, internal to this assembly, with a common object file format (COFF)-based image containing an emitted module, or a resource file. The raw bytes representing the symbols for the module are also loaded.

LoadModule(String, Byte[])

Loads the module, internal to this assembly, with a common object file format (COFF)-based image containing an emitted module, or a resource file.

LoadWithPartialName(String, Evidence)
Obsolete.

Loads an assembly from the application directory or from the global assembly cache using a partial name. The assembly is loaded using the supplied evidence.

LoadWithPartialName(String)
Obsolete.
Obsolete.
Obsolete.

Loads an assembly from the application directory or from the global assembly cache using a partial name.

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ReflectionOnlyLoad(Byte[])
Obsolete.

Loads the assembly from a common object file format (COFF)-based image containing an emitted assembly. The assembly is loaded into the reflection-only context of the caller's application domain.

ReflectionOnlyLoad(String)
Obsolete.

Loads an assembly into the reflection-only context, given its display name.

ReflectionOnlyLoadFrom(String)
Obsolete.

Loads an assembly into the reflection-only context, given its path.

SetEntryAssembly(Assembly)

Sets the application's entry assembly to the provided assembly object.

ToString()

Returns the full name of the assembly, also known as the display name.

UnsafeLoadFrom(String)

Loads an assembly into the load-from context, bypassing some security checks.

Operators

Equality(Assembly, Assembly)

Indicates whether two Assembly objects are equal.

Inequality(Assembly, Assembly)

Indicates whether two Assembly objects are not equal.

Events

ModuleResolve

Occurs when the common language runtime class loader cannot resolve a reference to an internal module of an assembly through normal means.

Explicit Interface Implementations

_Assembly.GetType()

Returns the type of the current instance.

ICustomAttributeProvider.GetCustomAttributes(Boolean)

Returns an array of all of the custom attributes defined on this member, excluding named attributes, or an empty array if there are no custom attributes.

ICustomAttributeProvider.GetCustomAttributes(Type, Boolean)

Returns an array of custom attributes defined on this member, identified by type, or an empty array if there are no custom attributes of that type.

ICustomAttributeProvider.IsDefined(Type, Boolean)

Indicates whether one or more instance of attributeType is defined on this member.

Extension Methods

GetExportedTypes(Assembly)
GetModules(Assembly)
GetTypes(Assembly)
GetCustomAttribute(Assembly, Type)

Retrieves a custom attribute of a specified type that is applied to a specified assembly.

GetCustomAttribute<T>(Assembly)

Retrieves a custom attribute of a specified type that is applied to a specified assembly.

GetCustomAttributes(Assembly, Type)

Retrieves a collection of custom attributes of a specified type that are applied to a specified assembly.

GetCustomAttributes(Assembly)

Retrieves a collection of custom attributes that are applied to a specified assembly.

GetCustomAttributes<T>(Assembly)

Retrieves a collection of custom attributes of a specified type that are applied to a specified assembly.

IsDefined(Assembly, Type)

Indicates whether custom attributes of a specified type are applied to a specified assembly.

TryGetRawMetadata(Assembly, Byte*, Int32)

Retrieves the metadata section of the assembly, for use with MetadataReader.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Thread Safety

This type is thread safe.

See also