Assembly.Load 方法

定义

加载程序集。

重载

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

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

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

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

Load(String, Evidence)
已过时.

在给定程序集的显示名称并使用提供的证据的情况下加载程序集。

Load(AssemblyName, Evidence)
已过时.

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

Load(Byte[])

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

Load(String)

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

Load(AssemblyName)

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

Load(Byte[], Byte[])

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

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

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

public:
 static System::Reflection::Assembly ^ Load(cli::array <System::Byte> ^ rawAssembly, cli::array <System::Byte> ^ rawSymbolStore, System::Security::SecurityContextSource securityContextSource);
public static System.Reflection.Assembly Load (byte[] rawAssembly, byte[] rawSymbolStore, System.Security.SecurityContextSource securityContextSource);
static member Load : byte[] * byte[] * System.Security.SecurityContextSource -> System.Reflection.Assembly
Public Shared Function Load (rawAssembly As Byte(), rawSymbolStore As Byte(), securityContextSource As SecurityContextSource) As Assembly

参数

rawAssembly
Byte[]

字节数组,它是包含已发出程序集的基于 COFF 的映像。

rawSymbolStore
Byte[]

包含表示程序集符号的原始字节的字节数组。

securityContextSource
SecurityContextSource

安全上下文的源。

返回

加载的程序集。

例外

rawAssemblynull

rawAssembly 对于当前加载的运行时,不是有效的程序集。

securityContextSource 的值不是枚举值之一。

注解

使用安全上下文的指定源将程序集加载到调用方的应用程序域中。 如果 rawSymbolStore 指定了 ,则还会加载表示程序集符号的原始字节。

适用于

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

注意

This method is obsolete and will be removed in a future release of the .NET Framework. Please use an overload of Load which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.

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

public:
 static System::Reflection::Assembly ^ Load(cli::array <System::Byte> ^ rawAssembly, cli::array <System::Byte> ^ rawSymbolStore, System::Security::Policy::Evidence ^ securityEvidence);
public static System.Reflection.Assembly Load (byte[] rawAssembly, byte[] rawSymbolStore, System.Security.Policy.Evidence securityEvidence);
[System.Obsolete("This method is obsolete and will be removed in a future release of the .NET Framework. Please use an overload of Load which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public static System.Reflection.Assembly Load (byte[] rawAssembly, byte[] rawSymbolStore, System.Security.Policy.Evidence securityEvidence);
static member Load : byte[] * byte[] * System.Security.Policy.Evidence -> System.Reflection.Assembly
[<System.Obsolete("This method is obsolete and will be removed in a future release of the .NET Framework. Please use an overload of Load which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")>]
static member Load : byte[] * byte[] * System.Security.Policy.Evidence -> System.Reflection.Assembly
Public Shared Function Load (rawAssembly As Byte(), rawSymbolStore As Byte(), securityEvidence As Evidence) As Assembly

参数

rawAssembly
Byte[]

字节数组,它是包含已发出程序集的基于 COFF 的映像。

rawSymbolStore
Byte[]

包含表示程序集符号的原始字节的字节数组。

securityEvidence
Evidence

用于加载程序集的证据。

返回

加载的程序集。

属性

例外

rawAssemblynull

rawAssembly 对于当前加载的运行时,不是有效的程序集。

一个程序集或模块用两个不同的证据加载了两次。

securityEvidence 不是 null。 默认情况下,在 .NET Framework 4 中不启用旧 CAS 策略;如果未启用,securityEvidence则必须为 null

注解

此方法重载始终在其自己的独立负载上下文中创建新 Assembly 对象。

使用提供的证据加载程序集。 还加载表示程序集符号的原始字节。

是否将特定的权限授予程序集是基于证据的。 程序集和安全证据合并的规则如下:

  • 使用 Load 没有 Evidence 参数的方法时,程序集会加载加载程序提供的证据。

  • 将 方法与参数一LoadEvidence起使用时,将合并证据片段。 作为方法参数 Load 提供的证据片段取代加载程序提供的证据片段。

  • Load 方法重载与参数一 Byte[] 起使用以加载 COFF 图像时,证据将从调用程序集继承。

  • 使用 Load 具有 Byte[] 参数 的方法和 Evidence 加载 COFF 图像时,仅使用提供的证据。 调用程序集的证据和 COFF 映像的证据将被忽略。

如果在同一程序集上多次调用 Load 方法,但指定了不同的证据,则公共语言运行时不会引发 , FileLoadException 因为无法确定不同证据规范的相等性和完整性。 第一个成功的证据是使用的证据。

适用于

Load(String, Evidence)

注意

This method is obsolete and will be removed in a future release of the .NET Framework. Please use an overload of Load which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.

在给定程序集的显示名称并使用提供的证据的情况下加载程序集。

public:
 static System::Reflection::Assembly ^ Load(System::String ^ assemblyString, System::Security::Policy::Evidence ^ assemblySecurity);
public static System.Reflection.Assembly Load (string assemblyString, System.Security.Policy.Evidence assemblySecurity);
[System.Obsolete("This method is obsolete and will be removed in a future release of the .NET Framework. Please use an overload of Load which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public static System.Reflection.Assembly Load (string assemblyString, System.Security.Policy.Evidence assemblySecurity);
static member Load : string * System.Security.Policy.Evidence -> System.Reflection.Assembly
[<System.Obsolete("This method is obsolete and will be removed in a future release of the .NET Framework. Please use an overload of Load which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")>]
static member Load : string * System.Security.Policy.Evidence -> System.Reflection.Assembly
Public Shared Function Load (assemblyString As String, assemblySecurity As Evidence) As Assembly

参数

assemblyString
String

程序集的显示名称。

assemblySecurity
Evidence

用于加载程序集的证据。

返回

加载的程序集。

属性

例外

assemblyStringnull

assemblyString 未找到。

assemblyString 对于当前加载的运行时,不是有效的程序集。

无法加载已找到的文件。

- 或 -

一个程序集或模块用两个不同的证据加载了两次。

注解

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

是否将特定的权限授予程序集是基于证据的。 程序集和安全证据合并的规则如下:

  • 使用 Load 没有 Evidence 参数的方法时,程序集会加载加载程序提供的证据。

  • 将 方法与参数一LoadEvidence起使用时,将合并证据片段。 作为方法参数 Load 提供的证据片段取代加载程序提供的证据片段。

  • Load 方法重载与参数一起使用 Byte[] 以加载 COFF) 图像 (通用对象文件格式时,证据继承自调用程序集。

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

如果在同一程序集上多次调用此方法,但指定了不同的证据,则公共语言运行时不会引发 , FileLoadException 因为无法确定不同证据规范的相等性和完整性。 第一个成功的证据是使用的证据。

在.NET Framework版本 2.0 中,处理器体系结构已添加到程序集标识,并且可以指定为程序集名称字符串的一部分。 例如,“ProcessorArchitecture=msil”。 但是,指定程序集名称的建议方法是创建 AssemblyName 对象并将其传递给 方法的适当 Load 重载。 请参阅 AssemblyName.ProcessorArchitecture

另请参阅

适用于

Load(AssemblyName, Evidence)

注意

This method is obsolete and will be removed in a future release of the .NET Framework. Please use an overload of Load which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.

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

public:
 static System::Reflection::Assembly ^ Load(System::Reflection::AssemblyName ^ assemblyRef, System::Security::Policy::Evidence ^ assemblySecurity);
public static System.Reflection.Assembly Load (System.Reflection.AssemblyName assemblyRef, System.Security.Policy.Evidence assemblySecurity);
[System.Obsolete("This method is obsolete and will be removed in a future release of the .NET Framework. Please use an overload of Load which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public static System.Reflection.Assembly Load (System.Reflection.AssemblyName assemblyRef, System.Security.Policy.Evidence assemblySecurity);
static member Load : System.Reflection.AssemblyName * System.Security.Policy.Evidence -> System.Reflection.Assembly
[<System.Obsolete("This method is obsolete and will be removed in a future release of the .NET Framework. Please use an overload of Load which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")>]
static member Load : System.Reflection.AssemblyName * System.Security.Policy.Evidence -> System.Reflection.Assembly
Public Shared Function Load (assemblyRef As AssemblyName, assemblySecurity As Evidence) As Assembly

参数

assemblyRef
AssemblyName

描述要加载的程序集的对象。

assemblySecurity
Evidence

用于加载程序集的证据。

返回

加载的程序集。

属性

例外

assemblyRefnull

assemblyRef 未找到。

assemblyRef 对于当前加载的运行时,不是有效的程序集。

一个程序集或模块用两个不同的证据加载了两次。

- 或 -

assemblyRef 指定远程程序集,但在远程程序集中执行代码的功能已禁用。 请参阅 <loadFromRemoteSources>

注解

FileLoadException 如果 assemblyRef 指定完整的程序集名称,并且与简单名称匹配的第一个程序集具有不同的版本、区域性或公钥标记,则引发 。 加载程序不会继续探测与简单名称匹配的其他程序集。 从 .NET Framework 4 开始,由于默认禁用远程程序集中的代码执行,FileLoadException如果assemblyRef指定远程程序集,也会引发 。 若要启用从远程位置加载的代码的执行,可以使用 <loadFromRemoteSources> 配置元素。

注意

不要仅使用 AssemblyName 属性集的 CodeBase 。 属性 CodeBase 不提供程序集标识 (的任何元素(如名称或版本) ),因此不会按照按标识加载规则进行加载,正如方法所期望 Load 的那样。 相反,程序集是使用 load-from 规则加载的。 有关使用从上下文加载的缺点的信息,请参阅 Assembly.LoadFrom 方法重载或 程序集加载的最佳做法

是否将特定的权限授予程序集是基于证据的。 程序集和安全证据合并的规则如下:

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

  • 将方法与 参数一LoadEvidence起使用时,将合并证据片段。 作为方法参数 Load 提供的证据片段取代加载程序提供的证据片段。

  • Load 方法重载与 参数一起使用 Byte[] 以加载 COFF) 图像 (通用对象文件格式时,证据继承自调用程序集。

  • 使用 LoadByte[] 参数的方法和 Evidence 加载 COFF 图像时,仅使用提供的证据。 将忽略调用程序集的证据和 COFF 映像的证据。

注意

如果同时 AssemblyName.Name 设置了 属性和 AssemblyName.CodeBase 属性,则首次尝试加载程序集将使用显示名称 (包括版本、区域性等,如属性) 返回 Assembly.FullName 。 如果未找到该文件, CodeBase 则用于搜索程序集。 如果使用 找到 CodeBase程序集,则显示名称与程序集匹配。 如果匹配失败, FileLoadException 则会引发 。

如果在同一程序集上多次调用 Load 方法,但指定了不同的证据,则公共语言运行时不会引发 , FileLoadException 因为无法确定不同证据规范的相等性和完整性。 第一个成功的证据是使用的证据。

另请参阅

适用于

Load(Byte[])

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

public:
 static System::Reflection::Assembly ^ Load(cli::array <System::Byte> ^ rawAssembly);
public static System.Reflection.Assembly Load (byte[] rawAssembly);
static member Load : byte[] -> System.Reflection.Assembly
Public Shared Function Load (rawAssembly As Byte()) As Assembly

参数

rawAssembly
Byte[]

字节数组,它是包含已发出程序集的基于 COFF 的映像。

返回

加载的程序集。

例外

rawAssemblynull

rawAssembly 不是当前加载的运行时的有效程序集。

注解

此方法重载始终在其自己的独立加载上下文中创建新 Assembly 对象。

仅.NET Framework:使用此方法加载的程序集的信任级别与调用程序集的信任级别相同。 若要从具有应用程序域信任级别的字节数组加载程序集,请使用 Load(Byte[], Byte[], SecurityContextSource) 方法重载。 有关在采用字节数组的方法重 Load 载中使用证据的详细信息,请参阅 Load(Byte[], Byte[], Evidence) 方法重载。

适用于

Load(String)

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

public:
 static System::Reflection::Assembly ^ Load(System::String ^ assemblyString);
public static System.Reflection.Assembly Load (string assemblyString);
static member Load : string -> System.Reflection.Assembly
Public Shared Function Load (assemblyString As String) As Assembly

参数

assemblyString
String

程序集名称的长格式或短格式。

返回

加载的程序集。

例外

assemblyStringnull

assemblyString 是一个长度为零的字符串。

assemblyString 未找到。

无法加载已找到的文件。

assemblyString 不是当前加载的运行时的有效程序集。

示例

以下示例根据程序集的完全限定名称加载程序集,并列出指定程序集中包含的所有类型。 有关如何获取完全限定程序集名称的信息,请参阅 程序集名称

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 );
    }
}
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);
        }
    }
}
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

注解

若要加载正确的程序集,建议通过传递程序集名称的长格式来调用 Load 方法。 程序集名称的长形式包括其简单名称 (例如 System.dll 程序集) 的“System”及其版本、区域性、公钥令牌及其处理器体系结构(可选)。 它对应于程序集的 FullName 属性。 以下示例演示如何使用长名称来加载 .NET Framework 4 的 System.dll 程序集:

using System;
using System.Reflection;

public class Example
{
   public static void Main()
   {
      string longName = "system, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
      Assembly assem = Assembly.Load(longName);
      if (assem == null)
         Console.WriteLine("Unable to load assembly...");
      else
         Console.WriteLine(assem.FullName);
   }
}
// The example displays the following output:
//        system, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Imports System.Reflection

Module Example
   Public Sub Main()
      Dim longName As String = "system, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
      Dim assem As Assembly = Assembly.Load(longName)
      If assem Is Nothing Then
         Console.WriteLine("Unable to load assembly...")
      Else
         Console.WriteLine(assem.FullName)
      End If
   End Sub
End Module
' The example displays the following output:
'       system, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

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

在 .NET Framework 版本 2.0 中,处理器体系结构已添加到程序集标识中,并且可以指定为程序集名称字符串的一部分。 例如,“ProcessorArchitecture=msil”。 但是,指定程序集名称的建议方法是创建对象 AssemblyName 并将其传递给 方法的适当 Load 重载。 请参阅 AssemblyName.ProcessorArchitecture

另请参阅

适用于

Load(AssemblyName)

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

public:
 static System::Reflection::Assembly ^ Load(System::Reflection::AssemblyName ^ assemblyRef);
public static System.Reflection.Assembly Load (System.Reflection.AssemblyName assemblyRef);
static member Load : System.Reflection.AssemblyName -> System.Reflection.Assembly
Public Shared Function Load (assemblyRef As AssemblyName) As Assembly

参数

assemblyRef
AssemblyName

描述要加载的程序集的对象。

返回

加载的程序集。

例外

assemblyRefnull

assemblyRef 未找到。

无法加载已找到的文件。

- 或 -

assemblyRef 指定远程程序集,但在远程程序集中执行代码的功能已禁用。 请参阅 <loadFromRemoteSources>

注意:在 适用于 Windows 应用商店应用的 .NET可移植类库中,改为捕获基类异常 IOException

assemblyRef 不是当前加载的运行时的有效程序集。

示例

以下示例实例化对象 AssemblyName ,并使用它来加载 sysglobal.dll 程序集。 然后,该示例显示程序集的公共类型的全名。

using System;
using System.Reflection;

public class Example
{
   public static void Main()
   {
      String fullName = "sysglobl, Version=4.0.0.0, Culture=neutral, " +
                        "PublicKeyToken=b03f5f7f11d50a3a, processor architecture=MSIL";
      var an = new AssemblyName(fullName);
      var assem = Assembly.Load(an);
      Console.WriteLine("Public types in assembly {0}:", assem.FullName);
      foreach (var t in assem.GetTypes())
         if (t.IsPublic)
            Console.WriteLine("   {0}", t.FullName);
   }
}
// The example displays the following output:
//   Public types in assembly sysglobl, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a:
//      System.Globalization.CultureAndRegionInfoBuilder
//      System.Globalization.CultureAndRegionModifiers
Imports System.Reflection

Module Example
   Public Sub Main()
      Dim fullName As String = "sysglobl, Version=4.0.0.0, Culture=neutral, " +
                               "PublicKeyToken=b03f5f7f11d50a3a, processor architecture=MSIL"
      Dim an As New AssemblyName(fullName)
      Dim assem As Assembly = Assembly.Load(an)
      Console.WriteLine("Public types in assembly {0}:", assem.FullName)
      For Each t As Type in assem.GetTypes()
         If t.IsPublic Then Console.WriteLine("   {0}", t.FullName)
      Next
   End Sub
End Module
' The example displays the following output:
'   Public types in assembly sysglobl, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a:
'      System.Globalization.CultureAndRegionInfoBuilder
'      System.Globalization.CultureAndRegionModifiers

注解

FileLoadException 如果 assemblyRef 指定了完整的程序集名称,并且与简单名称匹配的第一个程序集具有不同的版本、区域性或公钥标记,则引发 。 加载程序不会继续探测与简单名称匹配的其他程序集。 从 .NET Framework 4 开始,由于远程程序集中的代码执行默认处于禁用状态,FileLoadException如果assemblyRef指定远程程序集,也会引发 。 若要启用从远程位置加载的代码的执行,可以使用 <loadFromRemoteSources> 配置元素。

注意

不要仅CodeBase对 属性集使用 AssemblyName 。 属性 CodeBase 不提供程序集标识 (的任何元素(如名称或版本) ),因此不会按照按标识加载规则进行加载,正如方法所期望 Load 的那样。 相反,程序集是使用 load-from 规则加载的。 有关使用从上下文加载的缺点的信息,请参阅 Assembly.LoadFrom 方法重载或 程序集加载的最佳做法

注意

如果同时 AssemblyName.Name 设置了 属性和 AssemblyName.CodeBase 属性,则首次尝试加载程序集将使用显示名称 (包括版本、区域性等,如属性) 返回 Assembly.FullName 。 如果未找到该文件, CodeBase 则用于搜索程序集。 如果使用 找到 CodeBase程序集,则显示名称与程序集匹配。 如果匹配失败, FileLoadException 则会引发 。

另请参阅

适用于

Load(Byte[], Byte[])

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

public:
 static System::Reflection::Assembly ^ Load(cli::array <System::Byte> ^ rawAssembly, cli::array <System::Byte> ^ rawSymbolStore);
public static System.Reflection.Assembly Load (byte[] rawAssembly, byte[]? rawSymbolStore);
public static System.Reflection.Assembly Load (byte[] rawAssembly, byte[] rawSymbolStore);
static member Load : byte[] * byte[] -> System.Reflection.Assembly
Public Shared Function Load (rawAssembly As Byte(), rawSymbolStore As Byte()) As Assembly

参数

rawAssembly
Byte[]

字节数组,它是包含已发出程序集的基于 COFF 的映像。

rawSymbolStore
Byte[]

包含表示程序集符号的原始字节的字节数组。

返回

加载的程序集。

例外

rawAssemblynull

rawAssembly 不是当前加载的运行时的有效程序集。

注解

此方法重载始终在其自己的独立加载上下文中创建新 Assembly 对象。

仅.NET Framework:使用此方法加载的程序集的信任级别与调用程序集的信任级别相同。 若要从具有应用程序域信任级别的字节数组加载程序集,请使用 Load(Byte[], Byte[], SecurityContextSource) 方法重载。 有关在采用字节数组的方法重 Load 载中使用证据的详细信息,请参阅 Load(Byte[], Byte[], Evidence) 方法重载。

适用于