Assembly 类

定义

表示一个程序集,它是一个可重用、无版本冲突并且可自我描述的公共语言运行时应用程序构建基块。

public ref class Assembly abstract
public ref class Assembly abstract : System::Reflection::ICustomAttributeProvider, System::Runtime::Serialization::ISerializable
public ref class Assembly abstract : System::Reflection::ICustomAttributeProvider
public ref class Assembly : System::Reflection::ICustomAttributeProvider, System::Runtime::InteropServices::_Assembly, System::Runtime::Serialization::ISerializable, System::Security::IEvidenceFactory
public ref class Assembly abstract : System::Reflection::ICustomAttributeProvider, System::Runtime::InteropServices::_Assembly, System::Runtime::Serialization::ISerializable, System::Security::IEvidenceFactory
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
type Assembly = class
type Assembly = class
    interface ICustomAttributeProvider
    interface ISerializable
type Assembly = class
    interface ICustomAttributeProvider
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Serializable>]
type Assembly = class
    interface _Assembly
    interface IEvidenceFactory
    interface ICustomAttributeProvider
    interface ISerializable
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Assembly = class
    interface _Assembly
    interface IEvidenceFactory
    interface ICustomAttributeProvider
    interface ISerializable
Public MustInherit Class Assembly
Public MustInherit Class Assembly
Implements ICustomAttributeProvider, ISerializable
Public MustInherit Class Assembly
Implements ICustomAttributeProvider
Public Class Assembly
Implements _Assembly, ICustomAttributeProvider, IEvidenceFactory, ISerializable
Public MustInherit Class Assembly
Implements _Assembly, ICustomAttributeProvider, IEvidenceFactory, ISerializable
继承
Assembly
派生
属性
实现

示例

下面的代码示例演示如何获取当前正在执行的程序集、创建该程序集中包含的类型的实例,以及使用后期绑定调用该类型的方法之一。 为此,代码示例使用名为 的方法定义名为 ExampleSampleMethod类。 类的构造函数接受整数,该整数用于计算方法的返回值。

代码示例还演示了如何使用 GetName 方法获取 AssemblyName 可用于分析程序集全名的 对象。 该示例显示程序集的版本号、 CodeBase 属性和 EntryPoint 属性。

using namespace System;
using namespace System::Reflection;
using namespace System::Security::Permissions;

[assembly:AssemblyVersionAttribute("1.0.2000.0")];

public ref class Example
{
private: 
    int factor;

public:
    Example(int f)
    {
        factor = f;
    }

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

void main()
{
    Assembly^ assem = Example::typeid->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, 
        nullptr, gcnew array<Object^> { 2 }, nullptr, nullptr);

    // Make a late-bound call to an instance method of the object.    
    MethodInfo^ m = assem->GetType("Example")->GetMethod("SampleMethod");
    Object^ ret = m->Invoke(o, gcnew array<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/cpp/source.exe

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

Assembly entry point:
UInt32 _mainCRTStartup()
 */
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()
 */
Imports System.Reflection
Imports System.Security.Permissions

<assembly: AssemblyVersionAttribute("1.0.2000.0")>

Public Class Example
    Private factor As Integer
    
    Public Sub New(ByVal f As Integer) 
        factor = f
    End Sub 
    
    Public Function SampleMethod(ByVal x As Integer) As Integer 
        Console.WriteLine(vbCrLf & "Example.SampleMethod({0}) executes.", x)
        Return x * factor
    End Function 
    
    Public Shared Sub Main() 
        Dim assem As Assembly = GetType(Example).Assembly
        
        Console.WriteLine("Assembly Full Name:")
        Console.WriteLine(assem.FullName)
        
        ' The AssemblyName type can be used to parse the full name.
        Dim assemName As AssemblyName = assem.GetName()
        Console.WriteLine(vbLf + "Name: {0}", assemName.Name)
        Console.WriteLine("Version: {0}.{1}", assemName.Version.Major, _
            assemName.Version.Minor)
        
        Console.WriteLine(vbLf + "Assembly CodeBase:")
        Console.WriteLine(assem.CodeBase)
        
        ' Create an object from the assembly, passing in the correct number
        ' and type of arguments for the constructor.
        Dim o As Object = assem.CreateInstance("Example", False, _
            BindingFlags.ExactBinding, Nothing, _
            New Object() { 2 }, Nothing, Nothing)
        
        ' Make a late-bound call to an instance method of the object.    
        Dim m As MethodInfo = assem.GetType("Example").GetMethod("SampleMethod")
        Dim ret As Object = m.Invoke(o, New Object() { 42 })
        Console.WriteLine("SampleMethod returned {0}.", ret)
        
        Console.WriteLine(vbCrLf & "Assembly entry point:")
        Console.WriteLine(assem.EntryPoint)
    
    End Sub 
End Class 

' 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/vb/source.exe
'
'Example.SampleMethod(42) executes.
'SampleMethod returned 84.
'
'Assembly entry point:
'Void Main()
'

注解

Assembly使用 类加载程序集、浏览程序集的元数据和构成部分、发现程序集中包含的类型以及创建这些类型的实例。

若要获取表示当前加载到应用程序域 () 简单项目的默认应用程序域的程序集的对象的数组 Assembly ,请使用 AppDomain.GetAssemblies 方法。

为了动态加载程序集, Assembly 类在 Visual Basic) 中提供以下静态方法 (Shared 方法。 程序集将加载到发生加载操作的应用程序域中。

  • 建议的加载程序集方法是使用 Load 方法,该方法通过显示名称 (标识要加载的程序集,例如“System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”) 。 对程序集的搜索遵循 运行时如何查找程序集中所述的规则。

  • ReflectionOnlyLoadReflectionOnlyLoadFrom 方法使你能够加载程序集进行反射,但不能加载用于执行。 例如,面向 64 位平台的程序集可以通过在 32 位平台上运行的代码进行检查。

  • LoadFileLoadFrom 方法适用于必须按路径标识程序集的极少数情况。

若要获取 Assembly 当前正在执行的程序集的对象,请使用 GetExecutingAssembly 方法。

类的许多 Assembly 成员提供有关程序集的信息。 例如:

方法 GetTypes 列出程序集中的所有类型。 方法 GetExportedTypes 列出对程序集外部的调用方可见的类型。 方法 GetType 可用于在程序集中搜索特定类型。 方法 CreateInstance 可用于在程序集中搜索和创建类型的实例。

有关程序集的详细信息,请参阅应用程序域主题中的“ 应用程序域 和程序集”部分。

构造函数

Assembly()

初始化 Assembly 类的新实例。

属性

CodeBase
已过时.
已过时.

获取最初指定的程序集的位置,例如,在 AssemblyName 对象中指定的位置。

CustomAttributes

获取包含此程序集自定义属性的集合。

DefinedTypes

获取定义在此程序集中的类型的集合。

EntryPoint

获取此程序集的入口点。

EscapedCodeBase
已过时.
已过时.

获取 URI,包括表示基本代码的转义符。

Evidence

获取此程序集的证据。

ExportedTypes

获取此程序集中定义的公共类型的集合,这些公共类型在程序集外可见。

FullName

获取程序集的显示名称。

GlobalAssemblyCache
已过时.

获取一个值,该值指示程序集是否仅从全局程序集缓存加载 (.NET Framework) 。

HostContext

获取用于加载程序集的主机上下文。

ImageRuntimeVersion

获取表示公共语言运行时 (CLR) 的版本的字符串,该信息保存在包含清单的文件中。

IsCollectible

获取一个值,该值指示此程序集是否保留在可回收的 AssemblyLoadContext 中。

IsDynamic

获取一个值,该值指示当前程序集是否是通过使用反射发出在当前进程中动态生成的。

IsFullyTrusted

获取一个值,该值指示当前程序集是否是以完全信任方式加载的。

Location

获取包含清单的已加载文件的完整路径或 UNC 位置。

ManifestModule

获取包含当前程序集清单的模块。

Modules

获取包含此程序集中模块的集合。

PermissionSet

获取当前程序集的授予集。

ReflectionOnly

获取 Boolean 值,该值指示此程序集是否被加载到只反射上下文中。

SecurityRuleSet

获取一个值,该值指示公共语言运行时 (CLR) 对此程序集强制执行的安全规则集。

方法

CreateInstance(String)

使用区分大小写的搜索,从此程序集中查找指定的类型,然后使用系统激活器创建它的实例。

CreateInstance(String, Boolean)

使用可选的区分大小写搜索,从此程序集中查找指定的类型,然后使用系统激活器创建它的实例。

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

使用可选的区分大小写搜索并具有指定的区域性、参数和绑定及激活特性,从此程序集中查找指定的类型,并使用系统激活器创建它的实例。

CreateQualifiedName(String, String)

创建由类型的程序集的显示名称限定的类型的名称。

Equals(Object)

确定此程序集和指定的对象是否相等。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetAssembly(Type)

获取在其中定义指定类型的当前加载的程序集。

GetCallingAssembly()

返回方法(该方法调用当前正在执行的方法)的 Assembly

GetCustomAttributes(Boolean)

获取此程序集的所有自定义属性。

GetCustomAttributes(Type, Boolean)

获取按类型指定的此程序集的自定义属性。

GetCustomAttributesData()

返回有关已应用于当前 Assembly(表示为 CustomAttributeData 对象)的特性的信息。

GetEntryAssembly()

获取默认应用程序域中的进程可执行文件。 在其他的应用程序域中,这是由 ExecuteAssembly(String) 执行的第一个可执行文件。

GetExecutingAssembly()

获取包含当前执行的代码的程序集。

GetExportedTypes()

获取此程序集中定义的公共类型,这些公共类型在程序集外可见。

GetFile(String)

获取此程序集清单的文件表中指定文件的 FileStream

GetFiles()

获取程序集清单文件表中的文件。

GetFiles(Boolean)

获取程序集清单的文件表中的文件,指定是否包括资源模块。

GetForwardedTypes()

表示一个程序集,它是一个可重用、无版本冲突并且可自我描述的公共语言运行时应用程序构建基块。

GetHashCode()

返回此实例的哈希代码。

GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetLoadedModules()

获取作为此程序集的一部分的所有加载模块。

GetLoadedModules(Boolean)

获取属于此程序集的所有已加载模块,同时指定是否包括资源模块。

GetManifestResourceInfo(String)

返回关于给定资源如何保持的信息。

GetManifestResourceNames()

返回此程序集中的所有资源的名称。

GetManifestResourceStream(String)

从此程序集加载指定的清单资源。

GetManifestResourceStream(Type, String)

从此程序集加载指定清单资源,清单资源的范围由指定类型的命名空间确定。

GetModule(String)

获取此程序集中的指定模块。

GetModules()

获取作为此程序集的一部分的所有模块。

GetModules(Boolean)

获取属于此程序集的所有模块,同时指定是否包括资源模块。

GetName()

获取此程序集的 AssemblyName

GetName(Boolean)

获取此程序集的 AssemblyName,并按 copiedName 指定的那样设置基本代码。

GetObjectData(SerializationInfo, StreamingContext)
已过时.

获取序列化信息,其中包含重新实例化此程序集所需的所有数据。

GetReferencedAssemblies()

获取此程序集引用的所有程序集的 AssemblyName 对象。

GetSatelliteAssembly(CultureInfo)

获取指定区域性的附属程序集。

GetSatelliteAssembly(CultureInfo, Version)

获取指定区域性的附属程序集的指定版本。

GetType()

表示一个程序集,它是一个可重用、无版本冲突并且可自我描述的公共语言运行时应用程序构建基块。

GetType()

获取当前实例的 Type

(继承自 Object)
GetType(String)

获取程序集实例中具有指定名称的 Type 对象。

GetType(String, Boolean)

获取程序集实例中具有指定名称的 Type 对象,并选择在找不到该类型时引发异常。

GetType(String, Boolean, Boolean)

获取程序集实例中具有指定名称的 Type 对象,带有忽略大小写和在找不到该类型时引发异常的选项。

GetTypes()

获取此程序集中定义的所有类型。

IsDefined(Type, Boolean)

指示指定的属性是否已应用于该程序集。

Load(AssemblyName)

在给定程序集的 AssemblyName 的情况下,加载程序集。

Load(AssemblyName, Evidence)
已过时.

在给定程序集的 AssemblyName 的情况下,加载程序集。 使用提供的证据加载程序集。

Load(Byte[])

加载带有基于通用对象文件格式 (COFF) 的映像的程序集,该映像包含已发出的程序集。

Load(Byte[], Byte[])

加载带有基于通用对象文件格式 (COFF) 的映像的程序集,此映像包含一个已发出的程序集,并且还可以选择包括程序集的符号。

Load(Byte[], Byte[], Evidence)
已过时.

加载带有基于通用对象文件格式 (COFF) 的映像的程序集,此映像包含一个已发出的程序集,并且还可选择包括程序集的符号和证据。

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

加载带有基于通用对象文件格式 (COFF) 的映像的程序集,此映像包含一个已发出的程序集,并且还可选择包括符号和指定安全上下文的源。

Load(String)

用指定的名称加载程序集。

Load(String, Evidence)
已过时.

根据程序集的显示名称并使用提供的证据加载程序集。

LoadFile(String)

加载指定路径上的程序集文件的内容。

LoadFile(String, Evidence)
已过时.

根据程序集的路径加载程序集,使用提供的证据加载程序集。

LoadFrom(String)

已知程序集的文件名或路径,加载程序集。

LoadFrom(String, Byte[], AssemblyHashAlgorithm)

通过给定程序集文件名或路径、哈希值及哈希算法来加载程序集。

LoadFrom(String, Evidence)
已过时.

在给定程序集的文件名或路径并提供安全证据的情况下,加载程序集。

LoadFrom(String, Evidence, Byte[], AssemblyHashAlgorithm)
已过时.

通过给定程序集文件名或路径、安全证据、哈希值及哈希算法来加载程序集。

LoadModule(String, Byte[])

加载带有基于通用对象文件格式 (COFF) 的映像(包含已发出的模块)或资源文件的模块(该模块相对于此程序集是内部的)。

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

加载带有基于通用对象文件格式 (COFF) 的映像(包含已发出的模块)或资源文件的模块(该模块相对于此程序集是内部的)。 还加载表示此模块的符号的原始字节。

LoadWithPartialName(String)
已过时.
已过时.
已过时.

使用部分名称从应用程序目录或从全局程序集缓存加载程序集。

LoadWithPartialName(String, Evidence)
已过时.

使用部分名称从应用程序目录或从全局程序集缓存加载程序集。 使用提供的证据加载程序集。

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ReflectionOnlyLoad(Byte[])
已过时.

加载来自基于通用对象文件格式 (COFF) 的映像的程序集,该映像包含已发出的程序集。 程序集被加载到调用方的应用程序域的只反射上下文中。

ReflectionOnlyLoad(String)
已过时.

将给定显示名称的程序集加载到只反射上下文中。

ReflectionOnlyLoadFrom(String)
已过时.

将给定路径的程序集加载到只反射上下文中。

ToString()

返回程序集的全名,即所谓的显示名称。

UnsafeLoadFrom(String)

绕过某些安全检查,将程序集加载到加载源上下文中。

运算符

Equality(Assembly, Assembly)

指示两个 Assembly 对象是否相等。

Inequality(Assembly, Assembly)

指示两个 Assembly 对象是否不相等。

事件

ModuleResolve

当公共语言运行时类加载程序不能通过正常方法解析对程序集的内部模块的引用时发生。

显式接口实现

_Assembly.GetType()

返回当前实例的类型。

ICustomAttributeProvider.GetCustomAttributes(Boolean)

返回在该成员上定义的所有自定义特性的数组(已命名的特性除外),如果没有自定义特性,则返回空数组。

ICustomAttributeProvider.GetCustomAttributes(Type, Boolean)

返回在该成员上定义、由类型标识的自定义属性数组,如果没有该类型的自定义属性,则返回空数组。

ICustomAttributeProvider.IsDefined(Type, Boolean)

指示是否在该成员上定义了一个或多个 attributeType 实例。

扩展方法

GetExportedTypes(Assembly)

表示一个程序集,它是一个可重用、无版本冲突并且可自我描述的公共语言运行时应用程序构建基块。

GetModules(Assembly)

表示一个程序集,它是一个可重用、无版本冲突并且可自我描述的公共语言运行时应用程序构建基块。

GetTypes(Assembly)

表示一个程序集,它是一个可重用、无版本冲突并且可自我描述的公共语言运行时应用程序构建基块。

GetCustomAttribute(Assembly, Type)

检索应用于指定程序集的指定类型的自定义特性。

GetCustomAttribute<T>(Assembly)

检索应用于指定程序集的指定类型的自定义特性。

GetCustomAttributes(Assembly)

检索应用于指定程序集的自定义特性集合。

GetCustomAttributes(Assembly, Type)

检索应用于指定程序集的指定类型的自定义特性集合

GetCustomAttributes<T>(Assembly)

检索应用于指定程序集的指定类型的自定义特性集合

IsDefined(Assembly, Type)

确定是否将指定类型的任何自定义属性应用于指定的程序集。

TryGetRawMetadata(Assembly, Byte*, Int32)

检索程序集的元数据部分,以便与 一起使用 MetadataReader

适用于

线程安全性

此类型是线程安全的。

另请参阅