.NET Framework 类库
Assembly..::.Load 方法 (String)

更新:2007 年 11 月

通过给定程序集的长格式名称加载程序集。

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

语法

Visual Basic(声明)
Public Shared Function Load ( _
    assemblyString As String _
) As Assembly
Visual Basic (用法)
Dim assemblyString As String
Dim returnValue As [Assembly]

returnValue = Assembly.Load(assemblyString)
C#
public static Assembly Load(
    string assemblyString
)
Visual C++
public:
static Assembly^ Load(
    String^ assemblyString
)
J#
public static Assembly Load(
    String assemblyString
)
JScript
public static function Load(
    assemblyString : String
) : Assembly

参数

assemblyString
类型:System..::.String

程序集名称的长格式。

返回值

类型:System.Reflection..::.Assembly

加载的程序集。

异常

异常条件
ArgumentNullException

assemblyStringnullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing

ArgumentException

assemblyString 是零长度字符串。

FileNotFoundException

未找到 assemblyString

FileLoadException

发现一个未能加载的文件。

BadImageFormatException

assemblyString 不是有效程序集。

- 或 -

当前正在加载公共语言运行库的 2.0 版或更高版本,assemblyString 是使用更高版本进行编译的。

备注

如果 assemblyString 指定程序集的全名并且与简单名称匹配的第一个程序集具有不同的版本、区域性或公钥标记,则会引发 FileLoadException。加载程序不会继续探测与简单名称匹配的其他程序集。

是否向程序集授予一定的权限要视证据而定。程序集和安全证据的合并规则如下所示:

  • 使用不带 Evidence 参数的 Load 方法时,将用加载程序提供的证据加载程序集。

  • 使用带有 Evidence 参数的 Load 方法时,将对各项证据进行合并。作为 Load 方法的参数提供的各项证据将替代由加载程序提供的各项证据。

  • 使用带有 Byte[] 参数的 Load 方法重载加载通用对象文件格式 (COFF) 图像时,将从调用程序集继承证据。这适用于 .NET Framework 1.1 版 Service Pack 1 (SP1) 和后续版本。

    说明:

    在 .NET Framework 1.0 版和 1.1 版(不带 SP1)中,使用带有 Byte[] 参数的 Load 方法重载加载 COFF 图像时,将对证据进行合并。ZoneUrlSite 从调用程序集继承,而 HashStrongName 从 COFF 程序集中获取。

  • 使用带有 Byte[] 参数和 EvidenceLoad 方法加载 COFF 图像时,只使用提供的证据。将忽略调用程序集和 COFF 图像的证据。

在 C++ 可执行文件上反射可能会引发 BadImageFormatException。造成这种情况的原因极有可能是 C++ 编译器去除了可执行文件中的重定位地址或 .reloc 部分。要保留 C++ 可执行文件的 .reloc 地址,请在链接时指定 /fixed:no

在 .NET Framework 2.0 版中,处理器架构被添加到程序集标识中,可以指定为程序集名称字符串的一部分。例如,“ProcessorArchitecture=msil”。但是,指定程序集名称的推荐方式是创建一个 AssemblyName 对象,并将该对象传递给 Load 方法的相应重载。请参见 AssemblyName..::.ProcessorArchitecture

示例

下面的示例通过给定程序集的完全限定名加载程序集,并列出指定程序集中包含的所有类型。要运行该代码实例,必须提供完全限定程序集名称。有关如何获取完全限定程序集名称的信息,请参见程序集名称

Visual Basic
Imports System
Imports System.Reflection

Class Class1
    Public Shared Sub Main()
        ' You must supply a valid fully qualified assembly name.            
        Dim SampleAssembly As [Assembly] = _
            [Assembly].Load("SampleAssembly, Version=1.0.2004.0, Culture=neutral, PublicKeyToken=8744b20f8da049e3")
        Dim oType As Type
        ' Display all the types contained in the specified assembly.
        For Each oType In SampleAssembly.GetTypes()
            Console.WriteLine(oType.Name)
        Next oType
    End Sub    'LoadSample
End Class 'Class1
C#
using System;
using System.Reflection;

class Class1
{
    public static void Main()
    {
        // You must supply a valid fully qualified assembly name.            
        Assembly SampleAssembly = Assembly.Load
            ("SampleAssembly, Version=1.0.2004.0, Culture=neutral, PublicKeyToken=8744b20f8da049e3");
        // Display all the types contained in the specified assembly.
        foreach (Type oType in SampleAssembly.GetTypes()) {
            Console.WriteLine(oType.Name);
        }
    }
}
Visual C++
using namespace System;
using namespace System::Collections;
using namespace System::Reflection;
int main()
{
    // You must supply a valid fully qualified assembly name.            
    Assembly^ SampleAssembly = Assembly::Load
        ( "SampleAssembly, Version=1.0.2004.0, Culture=neutral, PublicKeyToken=8744b20f8da049e3" );
    array<Type^>^ Types = SampleAssembly->GetTypes();

    // Display all the types contained in the specified assembly.
    IEnumerator^ myEnum = Types->GetEnumerator();
    Type^ oType;
    while ( myEnum->MoveNext() )
    {
        oType = safe_cast<Type^>(myEnum->Current);
        Console::WriteLine( oType->Name );
    }
}

权限

平台

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

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

版本信息

.NET Framework

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

.NET Compact Framework

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

XNA Framework

受以下版本支持:2.0、1.0
另请参见

参考

其他资源

标记 :


Page view tracker