System.AppDomain.Load 方法 (Byte[], Byte[], Evidence)

方法描述

加载带有基于通用对象文件格式 (COFF) 的图像的 Assembly,该图像包含已发出的 Assembly。 还加载表示 Assembly 的符号的原始字节。

语法定义(C# System.AppDomain.Load 方法 (Byte[], Byte[], Evidence) 的用法)

[ObsoleteAttribute("Methods which use evidence to sandbox are 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.")]
[SecurityPermissionAttribute(SecurityAction.Demand, ControlEvidence = true)]
public Assembly Load(
	byte[] rawAssembly,
	byte[] rawSymbolStore,
	Evidence securityEvidence
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
rawAssembly System-Byte[] byte 类型的数组,它是包含已发出程序集的基于 COFF 的图像。
rawSymbolStore System-Byte[] byte 类型的数组,它包含表示程序集符号的原始字节。
securityEvidence System-Security-Policy-Evidence 用于加载程序集的证据。
返回值 System.Reflection.Assembly 加载的程序集。

提示和注释

从 .NET Framework 4 版 开始,使用此方法加载的程序集的信任级别与应用程序域的信任级别相同。

此方法只应用于将程序集加载到当前应用程序域中。 此方法为方便无法调用静态 Assembly.Load 方法的互操作调用方而提供。 要将程序集加载到其他应用程序域中年,请使用诸如 CreateInstanceAndUnwrap 的方法。

有关此方法的所有重载共有的信息,请参见 Load(AssemblyName) 方法重载。

System.AppDomain.Load 方法 (Byte[], Byte[], Evidence)例子

有关如何获取完全限定程序集名称的信息,请参见程序集名称。

using System;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;

class Test {
   public static void Main() {
      AppDomain currentDomain = AppDomain.CurrentDomain;

      InstantiateMyType(currentDomain);   // Failed!

      currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolver);

      InstantiateMyType(currentDomain);   // OK!
   }

   static void InstantiateMyType(AppDomain domain) {
      try {
	 // You must supply a valid fully qualified assembly name here.
         domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType");
      } catch (Exception e) {
         Console.WriteLine(e.Message);
      }
   }

   // Loads the content of a file to a byte array. 
   static byte[] loadFile(string filename) {
      FileStream fs = new FileStream(filename, FileMode.Open);
      byte[] buffer = new byte[(int) fs.Length];
      fs.Read(buffer, 0, buffer.Length);
      fs.Close();

      return buffer;
   }   

   static Assembly MyResolver(object sender, ResolveEventArgs args) {
      AppDomain domain = (AppDomain) sender;

      // Once the files are generated, this call is
      // actually no longer necessary.
      EmitAssembly(domain);

      byte[] rawAssembly = loadFile("temp.dll");
      byte[] rawSymbolStore = loadFile("temp.pdb");
      Assembly assembly = domain.Load(rawAssembly, rawSymbolStore);

      return assembly;
   }

   // Creates a dynamic assembly with symbol information
   // and saves them to temp.dll and temp.pdb
   static void EmitAssembly(AppDomain domain) {
      AssemblyName assemblyName = new AssemblyName();
      assemblyName.Name = "MyAssembly";

      AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Save);
      ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MyModule", "temp.dll", true);
      TypeBuilder typeBuilder = moduleBuilder.DefineType("MyType", TypeAttributes.Public);

      ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null);
      ILGenerator ilGenerator = constructorBuilder.GetILGenerator();
      ilGenerator.EmitWriteLine("MyType instantiated!");
      ilGenerator.Emit(OpCodes.Ret);

      typeBuilder.CreateType();

      assemblyBuilder.Save("temp.dll");
   }
}

异常

异常 异常描述
ArgumentNullException rawAssembly 为 null。
BadImageFormatException
  • rawAssembly 不是有效程序集。
  • 当前正在加载公共语言运行时的 2.0 版或更高版本,rawAssembly 是使用更高版本进行编译的。
AppDomainUnloadedException 尝试对已卸载的应用程序域进行操作。
FileLoadException 用两个不同的证据将一个程序集或模块加载了两次。
NotSupportedException securityEvidence 不是 null。 未启用旧版 CAS 策略时,securityEvidence 应该为 null。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:3.5、3.0、2.0、1.1、1.0 在 4 中过时(编译器警告) .NET Framework Client Profile 受以下版本支持:3.5 SP1 在 4 中过时(编译器警告)

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。