Assembly 类

定义一个 Assembly,它是可重用、无版本冲突并且可自我描述的公共语言运行库应用程序构造块。

**命名空间:**System.Reflection
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<SerializableAttribute> _
<ClassInterfaceAttribute(ClassInterfaceType.None)> _
<ComVisibleAttribute(True)> _
Public Class Assembly
    Implements _Assembly, IEvidenceFactory, ICustomAttributeProvider, ISerializable
用法
Dim instance As Assembly
[SerializableAttribute] 
[ClassInterfaceAttribute(ClassInterfaceType.None)] 
[ComVisibleAttribute(true)] 
public class Assembly : _Assembly, IEvidenceFactory, ICustomAttributeProvider, 
    ISerializable
[SerializableAttribute] 
[ClassInterfaceAttribute(ClassInterfaceType::None)] 
[ComVisibleAttribute(true)] 
public ref class Assembly : _Assembly, IEvidenceFactory, ICustomAttributeProvider, 
    ISerializable
/** @attribute SerializableAttribute() */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.None) */ 
/** @attribute ComVisibleAttribute(true) */ 
public class Assembly implements _Assembly, IEvidenceFactory, 
    ICustomAttributeProvider, ISerializable
SerializableAttribute 
ClassInterfaceAttribute(ClassInterfaceType.None) 
ComVisibleAttribute(true) 
public class Assembly implements _Assembly, IEvidenceFactory, 
    ICustomAttributeProvider, ISerializable

备注

程序集提供使运行库能够充分了解应用程序的内容并强制使用应用程序定义的版本控制和依赖项规则的结构。这些概念对解决版本控制问题和简化运行库应用程序的部署至关重要。

示例

下面的代码示例列出了程序集中每个方法的方法签名。

' LoadInvoke loads MyAssembly.dll and lists the method
' information for each method. After compiling this class,
' run LoadInvoke.exe with the DisplayName for the assembly,
' as shown here:
' LoadInvoke MyAssembly
Imports System
Imports System.Reflection
Imports System.Security.Permissions

Public Class LoadInvoke

    <PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
    Public Shared Sub Main(ByVal args() As String)
        Dim a As [Assembly] = [Assembly].Load(args(0))
        Dim mytypes As Type() = a.GetTypes()
        Dim flags As BindingFlags = BindingFlags.NonPublic Or BindingFlags.Public Or BindingFlags.Static Or _
            BindingFlags.Instance Or BindingFlags.DeclaredOnly

        Dim t As Type
        For Each t In mytypes
            Dim mi As MethodInfo() = t.GetMethods(flags)
            Dim obj As [Object] = Activator.CreateInstance(t)

            Dim m As MethodInfo
            For Each m In mi
                ' Instead of invoking the methods,
                ' it's safer to initially just list them.
                Console.WriteLine(m)
            Next m
        Next t
    End Sub 
End Class 
// LoadInvoke loads MyAssembly.dll and lists the method
// information for each method. After compiling this class,
// run LoadInvoke.exe with the DisplayName for the assembly,
// as shown here:
// LoadInvoke MyAssembly

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

public class LoadInvoke
{
    [PermissionSetAttribute(SecurityAction.Demand, Name="FullTrust")]
    public static void Main(string[] args)
    {
        Assembly a = Assembly.Load(args[0]);
        Type[] mytypes = a.GetTypes();
        BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public |
            BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);

        foreach(Type t in mytypes)
        {
            MethodInfo[] mi = t.GetMethods(flags);
            Object obj = Activator.CreateInstance(t);

            foreach(MethodInfo m in mi)
            {
                // Instead of invoking the methods,
                // it's safer to initially just list them.
                Console.WriteLine(m);
            }
        }
    }
}
' Use this class with the LoadInvoke program.
' Compile this class using vbc /t:library MyAssembly.vb
' to obtain MyAssembly.dll.
Imports System
Imports Microsoft.VisualBasic

Public Class MyAssembly
    Public Sub MyMethod1()
        Console.WriteLine("Invoking MyAssembly.MyMethod1")
    End Sub 'MyMethod1
End Class 'MyAssembly
// Use this class with the LoadInvoke program.
// Compile this class using "csc /t:library MyAssembly.cs"
// to build MyAssembly.dll.
using System;

public class MyAssembly
{
    public void MyMethod1()
    {
        Console.WriteLine("This is MyMethod1");
    }
    public void MyMethod2()
    {
        Console.WriteLine("This is MyMethod2");
    }
    public void MyMethod3()
    {
        Console.WriteLine("This is MyMethod3");
    }
}
// Use this class with the LoadInvoke program.
// Compile this class using csc /t:library MyAssembly.cs
// to obtain MyAssembly.dll.
using namespace System;
public ref class MyAssembly
{
public:
   void MyMethod1()
   {
      Console::WriteLine( "Invoking MyAssembly.MyMethod1" );
   }

};

继承层次结构

System.Object
  System.Reflection.Assembly
     System.Reflection.Emit.AssemblyBuilder

线程安全

该类型对于多线程操作是安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Assembly 成员
System.Reflection 命名空间