AppDomain.Load 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将 Assembly 加载到此应用程序域中。
重载
Load(Byte[]) | |
Load(AssemblyName) |
在给定 AssemblyName 的情况下加载 Assembly。 |
Load(String) |
在给定其显示名称的情况下加载 Assembly。 |
Load(Byte[], Byte[]) |
加载带有基于通用对象文件格式 (COFF) 的图像的 Assembly,该图像包含已发出的 Assembly。 还加载表示 Assembly 的符号的原始字节。 |
Load(AssemblyName, Evidence) |
已过时.
在给定 AssemblyName 的情况下加载 Assembly。 |
Load(String, Evidence) |
已过时.
在给定其显示名称的情况下加载 Assembly。 |
Load(Byte[], Byte[], Evidence) |
已过时.
加载带有基于通用对象文件格式 (COFF) 的图像的 Assembly,该图像包含已发出的 Assembly。 还加载表示 Assembly 的符号的原始字节。 |
Load(Byte[])
- Source:
- AppDomain.cs
- Source:
- AppDomain.cs
- Source:
- AppDomain.cs
public:
System::Reflection::Assembly ^ Load(cli::array <System::Byte> ^ rawAssembly);
public:
virtual System::Reflection::Assembly ^ Load(cli::array <System::Byte> ^ rawAssembly);
public System.Reflection.Assembly Load (byte[] rawAssembly);
member this.Load : byte[] -> System.Reflection.Assembly
abstract member Load : byte[] -> System.Reflection.Assembly
override this.Load : byte[] -> System.Reflection.Assembly
Public Function Load (rawAssembly As Byte()) As Assembly
参数
- rawAssembly
- Byte[]
byte
类型的数组,它是包含已发出程序集的基于 COFF 的图像。
返回
加载的程序集。
实现
例外
rawAssembly
为 null
。
rawAssembly
对于当前加载的运行时,不是有效的程序集。
在卸载的应用程序域上尝试该操作。
一个程序集或模块用两个不同的证据加载了两次。
示例
以下示例演示如何使用加载原始程序集。
若要运行此代码示例,必须提供完全限定的程序集名称。 有关如何获取完全限定程序集名称的信息,请参阅 程序集名称。
using namespace System;
using namespace System::IO;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
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.
array<Byte>^ loadFile( String^ filename )
{
FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
array<Byte>^buffer = gcnew array<Byte>((int)fs->Length);
fs->Read( buffer, 0, buffer->Length );
fs->Close();
return buffer;
}
// Creates a dynamic assembly with symbol information
// and saves them to temp.dll and temp.pdb
void EmitAssembly( AppDomain^ domain )
{
AssemblyName^ assemblyName = gcnew 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, nullptr );
ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
ilGenerator->EmitWriteLine( "MyType instantiated!" );
ilGenerator->Emit( OpCodes::Ret );
typeBuilder->CreateType();
assemblyBuilder->Save( "temp.dll" );
}
ref class Resolver
{
public:
static Assembly^ MyResolver( Object^ sender, ResolveEventArgs^ args )
{
AppDomain^ domain = dynamic_cast<AppDomain^>(sender);
// Once the files are generated, this call is
// actually no longer necessary.
EmitAssembly( domain );
array<Byte>^rawAssembly = loadFile( "temp.dll" );
array<Byte>^rawSymbolStore = loadFile( "temp.pdb" );
Assembly^ assembly = domain->Load( rawAssembly, rawSymbolStore );
return assembly;
}
};
int main()
{
AppDomain^ currentDomain = AppDomain::CurrentDomain;
InstantiateMyType( currentDomain ); // Failed!
currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Resolver::MyResolver );
InstantiateMyType( currentDomain ); // OK!
}
using System;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
class LoadRawSnippet {
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");
}
}
open System
open System.IO
open System.Reflection
open System.Reflection.Emit
let instantiateMyType (domain: AppDomain) =
try
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType")
|> ignore
with e ->
printfn $"{e.Message}"
// Loads the content of a file to a byte array.
let loadFile filename =
use fs = new FileStream(filename, FileMode.Open)
let buffer = Array.zeroCreate<byte> (int fs.Length)
fs.Read(buffer, 0, buffer.Length) |> ignore
fs.Close()
buffer
// Creates a dynamic assembly with symbol information
// and saves them to temp.dll and temp.pdb
let emitAssembly (domain: AppDomain) =
let assemblyName = AssemblyName()
assemblyName.Name <- "MyAssembly"
let assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Save)
let moduleBuilder = assemblyBuilder.DefineDynamicModule("MyModule", "temp.dll", true)
let typeBuilder = moduleBuilder.DefineType("MyType", TypeAttributes.Public)
let constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null)
let ilGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine "MyType instantiated!"
ilGenerator.Emit OpCodes.Ret
typeBuilder.CreateType() |> ignore
assemblyBuilder.Save "temp.dll"
let myResolver (sender: obj) (args: ResolveEventArgs) =
let domain = sender :?> AppDomain
// Once the files are generated, this call is
// actually no longer necessary.
emitAssembly domain
let rawAssembly = loadFile "temp.dll"
let rawSymbolStore = loadFile "temp.pdb"
domain.Load(rawAssembly, rawSymbolStore)
let currentDomain = AppDomain.CurrentDomain
instantiateMyType currentDomain // Failed!
currentDomain.add_AssemblyResolve (ResolveEventHandler myResolver)
instantiateMyType currentDomain // OK!
Imports System.IO
Imports System.Reflection
Imports System.Reflection.Emit
Module Test
Sub Main()
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
InstantiateMyType(currentDomain) ' Failed!
AddHandler currentDomain.AssemblyResolve, AddressOf MyResolver
InstantiateMyType(currentDomain) ' OK!
End Sub
Sub InstantiateMyType(domain As AppDomain)
Try
' You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType")
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub
' Loads the content of a file to a byte array.
Function loadFile(filename As String) As Byte()
Dim fs As New FileStream(filename, FileMode.Open)
Dim buffer(CInt(fs.Length - 1)) As Byte
fs.Read(buffer, 0, buffer.Length)
fs.Close()
Return buffer
End Function 'loadFile
Function MyResolver(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
Dim domain As AppDomain = DirectCast(sender, AppDomain)
' Once the files are generated, this call is
' actually no longer necessary.
EmitAssembly(domain)
Dim rawAssembly As Byte() = loadFile("temp.dll")
Dim rawSymbolStore As Byte() = loadFile("temp.pdb")
Dim myAssembly As System.Reflection.Assembly = domain.Load(rawAssembly, rawSymbolStore)
Return myAssembly
End Function 'MyResolver
' Creates a dynamic assembly with symbol information
' and saves them to temp.dll and temp.pdb
Sub EmitAssembly(domain As AppDomain)
Dim assemblyName As New AssemblyName()
assemblyName.Name = "MyAssembly"
Dim assemblyBuilder As AssemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Save)
Dim moduleBuilder As ModuleBuilder = assemblyBuilder.DefineDynamicModule("MyModule", "temp.dll", True)
Dim typeBuilder As TypeBuilder = moduleBuilder.DefineType("MyType", TypeAttributes.Public)
Dim constructorBuilder As ConstructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Nothing)
Dim ilGenerator As ILGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine("MyType instantiated!")
ilGenerator.Emit(OpCodes.Ret)
typeBuilder.CreateType()
assemblyBuilder.Save("temp.dll")
End Sub
End Module 'Test
注解
有关此方法的所有重载通用的信息,请参阅 Load(AssemblyName) 方法重载。
从 .NET Framework 4 开始,使用此方法加载的程序集的信任级别与应用程序域的信任级别相同。
适用于
Load(AssemblyName)
- Source:
- AppDomain.cs
- Source:
- AppDomain.cs
- Source:
- AppDomain.cs
在给定 AssemblyName 的情况下加载 Assembly。
public:
System::Reflection::Assembly ^ Load(System::Reflection::AssemblyName ^ assemblyRef);
public:
virtual System::Reflection::Assembly ^ Load(System::Reflection::AssemblyName ^ assemblyRef);
public System.Reflection.Assembly Load (System.Reflection.AssemblyName assemblyRef);
member this.Load : System.Reflection.AssemblyName -> System.Reflection.Assembly
abstract member Load : System.Reflection.AssemblyName -> System.Reflection.Assembly
override this.Load : System.Reflection.AssemblyName -> System.Reflection.Assembly
Public Function Load (assemblyRef As AssemblyName) As Assembly
参数
- assemblyRef
- AssemblyName
描述要加载的程序集的对象。
返回
加载的程序集。
实现
例外
assemblyRef
为 null
。
assemblyRef
未找到。
assemblyRef
对于当前加载的运行时,不是有效的程序集。
在卸载的应用程序域上尝试该操作。
一个程序集或模块用两个不同的证据加载了两次。
注解
此方法只能用于将程序集加载到当前应用程序域。 提供此方法是为了方便无法调用静态 Assembly.Load 方法的互操作性调用方。 若要将程序集加载到其他应用程序域,请使用 方法,例如 CreateInstanceAndUnwrap。
如果已加载所请求程序集的某个版本,则此方法将返回加载的程序集,即使请求了不同的版本。
不建议为 提供部分程序集名称 assemblyRef
。 (部分名称省略一个或多个区域性、版本或公钥令牌。对于采用字符串而不是 AssemblyName 对象的重载,“MyAssembly, Version=1.0.0.0”是部分名称的示例,“MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=18ab3442da84b47”是全名的示例。) 使用部分名称会对性能产生负面影响。 此外,仅当应用程序基目录中存在程序集的确切副本 (或 AppDomainSetup.ApplicationBase) BaseDirectory 时,部分程序集名称才能从全局程序集缓存加载程序集。
如果当前 AppDomain 对象表示应用程序域 A
,并且 Load 方法是从应用程序域 B
调用的,则程序集将加载到两个应用程序域中。 例如,以下代码将加载 MyAssembly
到新的应用程序域 ChildDomain
以及执行代码的应用程序域中:
AppDomain^ ad = AppDomain::CreateDomain("ChildDomain");
ad->Load("MyAssembly");
AppDomain ad = AppDomain.CreateDomain("ChildDomain");
ad.Load("MyAssembly");
let ad = AppDomain.CreateDomain "ChildDomain"
ad.Load "MyAssembly"
Dim ad As AppDomain = AppDomain.CreateDomain("ChildDomain")
ad.Load("MyAssembly")
程序集加载到这两个域中,因为 Assembly 不派生自 MarshalByRefObject,因此无法封送方法的 Load 返回值。 相反,公共语言运行时会尝试将程序集加载到调用应用程序域中。 如果两个应用程序域的路径设置不同,则加载到两个应用程序域中的程序集可能不同。
注意
如果同时 AssemblyName.Name 设置了 属性和 AssemblyName.CodeBase 属性,则第一次加载程序集的尝试使用显示名称 (包括版本、区域性等,) 属性返回 Assembly.FullName 。 如果未找到该文件,则 CodeBase 使用 属性搜索程序集。 如果使用 找到 CodeBase程序集,则显示名称与程序集匹配。 如果匹配失败, FileLoadException 则会引发 。
适用于
Load(String)
- Source:
- AppDomain.cs
- Source:
- AppDomain.cs
- Source:
- AppDomain.cs
在给定其显示名称的情况下加载 Assembly。
public:
System::Reflection::Assembly ^ Load(System::String ^ assemblyString);
public:
virtual System::Reflection::Assembly ^ Load(System::String ^ assemblyString);
public System.Reflection.Assembly Load (string assemblyString);
member this.Load : string -> System.Reflection.Assembly
abstract member Load : string -> System.Reflection.Assembly
override this.Load : string -> System.Reflection.Assembly
Public Function Load (assemblyString As String) As Assembly
参数
返回
加载的程序集。
实现
例外
assemblyString
为 null
assemblyString
未找到。
assemblyString
对于当前加载的运行时,不是有效的程序集。
在卸载的应用程序域上尝试该操作。
一个程序集或模块用两个不同的证据加载了两次。
注解
有关此方法的所有重载通用的信息,请参阅 Load(AssemblyName) 方法重载。
适用于
Load(Byte[], Byte[])
- Source:
- AppDomain.cs
- Source:
- AppDomain.cs
- Source:
- AppDomain.cs
public:
System::Reflection::Assembly ^ Load(cli::array <System::Byte> ^ rawAssembly, cli::array <System::Byte> ^ rawSymbolStore);
public:
virtual System::Reflection::Assembly ^ Load(cli::array <System::Byte> ^ rawAssembly, cli::array <System::Byte> ^ rawSymbolStore);
public System.Reflection.Assembly Load (byte[] rawAssembly, byte[]? rawSymbolStore);
public System.Reflection.Assembly Load (byte[] rawAssembly, byte[] rawSymbolStore);
member this.Load : byte[] * byte[] -> System.Reflection.Assembly
abstract member Load : byte[] * byte[] -> System.Reflection.Assembly
override this.Load : byte[] * byte[] -> System.Reflection.Assembly
Public Function Load (rawAssembly As Byte(), rawSymbolStore As Byte()) As Assembly
参数
- rawAssembly
- Byte[]
byte
类型的数组,它是包含已发出程序集的基于 COFF 的图像。
- rawSymbolStore
- Byte[]
byte
类型的数组,它包含表示程序集符号的原始字节。
返回
加载的程序集。
实现
例外
rawAssembly
为 null
。
rawAssembly
对于当前加载的运行时,不是有效的程序集。
在卸载的应用程序域上尝试该操作。
一个程序集或模块用两个不同的证据加载了两次。
示例
以下示例演示如何使用加载原始程序集。
若要运行此代码示例,必须提供完全限定的程序集名称。 有关如何获取完全限定程序集名称的信息,请参阅 程序集名称。
using namespace System;
using namespace System::IO;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
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.
array<Byte>^ loadFile( String^ filename )
{
FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
array<Byte>^buffer = gcnew array<Byte>((int)fs->Length);
fs->Read( buffer, 0, buffer->Length );
fs->Close();
return buffer;
}
// Creates a dynamic assembly with symbol information
// and saves them to temp.dll and temp.pdb
void EmitAssembly( AppDomain^ domain )
{
AssemblyName^ assemblyName = gcnew 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, nullptr );
ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
ilGenerator->EmitWriteLine( "MyType instantiated!" );
ilGenerator->Emit( OpCodes::Ret );
typeBuilder->CreateType();
assemblyBuilder->Save( "temp.dll" );
}
ref class Resolver
{
public:
static Assembly^ MyResolver( Object^ sender, ResolveEventArgs^ args )
{
AppDomain^ domain = dynamic_cast<AppDomain^>(sender);
// Once the files are generated, this call is
// actually no longer necessary.
EmitAssembly( domain );
array<Byte>^rawAssembly = loadFile( "temp.dll" );
array<Byte>^rawSymbolStore = loadFile( "temp.pdb" );
Assembly^ assembly = domain->Load( rawAssembly, rawSymbolStore );
return assembly;
}
};
int main()
{
AppDomain^ currentDomain = AppDomain::CurrentDomain;
InstantiateMyType( currentDomain ); // Failed!
currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Resolver::MyResolver );
InstantiateMyType( currentDomain ); // OK!
}
using System;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
class LoadRawSnippet {
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");
}
}
open System
open System.IO
open System.Reflection
open System.Reflection.Emit
let instantiateMyType (domain: AppDomain) =
try
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType")
|> ignore
with e ->
printfn $"{e.Message}"
// Loads the content of a file to a byte array.
let loadFile filename =
use fs = new FileStream(filename, FileMode.Open)
let buffer = Array.zeroCreate<byte> (int fs.Length)
fs.Read(buffer, 0, buffer.Length) |> ignore
fs.Close()
buffer
// Creates a dynamic assembly with symbol information
// and saves them to temp.dll and temp.pdb
let emitAssembly (domain: AppDomain) =
let assemblyName = AssemblyName()
assemblyName.Name <- "MyAssembly"
let assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Save)
let moduleBuilder = assemblyBuilder.DefineDynamicModule("MyModule", "temp.dll", true)
let typeBuilder = moduleBuilder.DefineType("MyType", TypeAttributes.Public)
let constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null)
let ilGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine "MyType instantiated!"
ilGenerator.Emit OpCodes.Ret
typeBuilder.CreateType() |> ignore
assemblyBuilder.Save "temp.dll"
let myResolver (sender: obj) (args: ResolveEventArgs) =
let domain = sender :?> AppDomain
// Once the files are generated, this call is
// actually no longer necessary.
emitAssembly domain
let rawAssembly = loadFile "temp.dll"
let rawSymbolStore = loadFile "temp.pdb"
domain.Load(rawAssembly, rawSymbolStore)
let currentDomain = AppDomain.CurrentDomain
instantiateMyType currentDomain // Failed!
currentDomain.add_AssemblyResolve (ResolveEventHandler myResolver)
instantiateMyType currentDomain // OK!
Imports System.IO
Imports System.Reflection
Imports System.Reflection.Emit
Module Test
Sub Main()
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
InstantiateMyType(currentDomain) ' Failed!
AddHandler currentDomain.AssemblyResolve, AddressOf MyResolver
InstantiateMyType(currentDomain) ' OK!
End Sub
Sub InstantiateMyType(domain As AppDomain)
Try
' You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType")
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub
' Loads the content of a file to a byte array.
Function loadFile(filename As String) As Byte()
Dim fs As New FileStream(filename, FileMode.Open)
Dim buffer(CInt(fs.Length - 1)) As Byte
fs.Read(buffer, 0, buffer.Length)
fs.Close()
Return buffer
End Function 'loadFile
Function MyResolver(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
Dim domain As AppDomain = DirectCast(sender, AppDomain)
' Once the files are generated, this call is
' actually no longer necessary.
EmitAssembly(domain)
Dim rawAssembly As Byte() = loadFile("temp.dll")
Dim rawSymbolStore As Byte() = loadFile("temp.pdb")
Dim myAssembly As System.Reflection.Assembly = domain.Load(rawAssembly, rawSymbolStore)
Return myAssembly
End Function 'MyResolver
' Creates a dynamic assembly with symbol information
' and saves them to temp.dll and temp.pdb
Sub EmitAssembly(domain As AppDomain)
Dim assemblyName As New AssemblyName()
assemblyName.Name = "MyAssembly"
Dim assemblyBuilder As AssemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Save)
Dim moduleBuilder As ModuleBuilder = assemblyBuilder.DefineDynamicModule("MyModule", "temp.dll", True)
Dim typeBuilder As TypeBuilder = moduleBuilder.DefineType("MyType", TypeAttributes.Public)
Dim constructorBuilder As ConstructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Nothing)
Dim ilGenerator As ILGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine("MyType instantiated!")
ilGenerator.Emit(OpCodes.Ret)
typeBuilder.CreateType()
assemblyBuilder.Save("temp.dll")
End Sub
End Module 'Test
注解
有关此方法的所有重载通用的信息,请参阅 Load(AssemblyName) 方法重载。
从 .NET Framework 4 开始,使用此方法加载的程序集的信任级别与应用程序域的信任级别相同。
适用于
Load(AssemblyName, Evidence)
注意
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.
在给定 AssemblyName 的情况下加载 Assembly。
public:
virtual System::Reflection::Assembly ^ Load(System::Reflection::AssemblyName ^ assemblyRef, System::Security::Policy::Evidence ^ assemblySecurity);
public System.Reflection.Assembly Load (System.Reflection.AssemblyName assemblyRef, System.Security.Policy.Evidence assemblySecurity);
[System.Obsolete("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.")]
public System.Reflection.Assembly Load (System.Reflection.AssemblyName assemblyRef, System.Security.Policy.Evidence assemblySecurity);
abstract member Load : System.Reflection.AssemblyName * System.Security.Policy.Evidence -> System.Reflection.Assembly
override this.Load : System.Reflection.AssemblyName * System.Security.Policy.Evidence -> System.Reflection.Assembly
[<System.Obsolete("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.")>]
abstract member Load : System.Reflection.AssemblyName * System.Security.Policy.Evidence -> System.Reflection.Assembly
override this.Load : System.Reflection.AssemblyName * System.Security.Policy.Evidence -> System.Reflection.Assembly
Public Function Load (assemblyRef As AssemblyName, assemblySecurity As Evidence) As Assembly
参数
- assemblyRef
- AssemblyName
描述要加载的程序集的对象。
- assemblySecurity
- Evidence
用于加载程序集的证据。
返回
加载的程序集。
实现
- 属性
例外
assemblyRef
为 null
assemblyRef
未找到。
assemblyRef
对于当前加载的运行时,不是有效的程序集。
在卸载的应用程序域上尝试该操作。
一个程序集或模块用两个不同的证据加载了两次。
注解
有关此方法的所有重载通用的信息,请参阅 Load(AssemblyName) 方法重载。
适用于
Load(String, Evidence)
注意
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.
在给定其显示名称的情况下加载 Assembly。
public:
virtual System::Reflection::Assembly ^ Load(System::String ^ assemblyString, System::Security::Policy::Evidence ^ assemblySecurity);
public System.Reflection.Assembly Load (string assemblyString, System.Security.Policy.Evidence assemblySecurity);
[System.Obsolete("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.")]
public System.Reflection.Assembly Load (string assemblyString, System.Security.Policy.Evidence assemblySecurity);
abstract member Load : string * System.Security.Policy.Evidence -> System.Reflection.Assembly
override this.Load : string * System.Security.Policy.Evidence -> System.Reflection.Assembly
[<System.Obsolete("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.")>]
abstract member Load : string * System.Security.Policy.Evidence -> System.Reflection.Assembly
override this.Load : string * System.Security.Policy.Evidence -> System.Reflection.Assembly
Public Function Load (assemblyString As String, assemblySecurity As Evidence) As Assembly
参数
- assemblySecurity
- Evidence
用于加载程序集的证据。
返回
加载的程序集。
实现
- 属性
例外
assemblyString
为 null
assemblyString
未找到。
assemblyString
对于当前加载的运行时,不是有效的程序集。
在卸载的应用程序域上尝试该操作。
一个程序集或模块用两个不同的证据加载了两次。
注解
有关此方法的所有重载共有的信息,请参阅 Load(AssemblyName) 方法重载。
适用于
Load(Byte[], Byte[], Evidence)
注意
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.
public:
virtual System::Reflection::Assembly ^ Load(cli::array <System::Byte> ^ rawAssembly, cli::array <System::Byte> ^ rawSymbolStore, System::Security::Policy::Evidence ^ securityEvidence);
public System.Reflection.Assembly Load (byte[] rawAssembly, byte[] rawSymbolStore, System.Security.Policy.Evidence securityEvidence);
[System.Obsolete("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.")]
public System.Reflection.Assembly Load (byte[] rawAssembly, byte[] rawSymbolStore, System.Security.Policy.Evidence securityEvidence);
abstract member Load : byte[] * byte[] * System.Security.Policy.Evidence -> System.Reflection.Assembly
override this.Load : byte[] * byte[] * System.Security.Policy.Evidence -> System.Reflection.Assembly
[<System.Obsolete("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.")>]
abstract member Load : byte[] * byte[] * System.Security.Policy.Evidence -> System.Reflection.Assembly
override this.Load : byte[] * byte[] * System.Security.Policy.Evidence -> System.Reflection.Assembly
Public Function Load (rawAssembly As Byte(), rawSymbolStore As Byte(), securityEvidence As Evidence) As Assembly
参数
- rawAssembly
- Byte[]
byte
类型的数组,它是包含已发出程序集的基于 COFF 的图像。
- rawSymbolStore
- Byte[]
byte
类型的数组,它包含表示程序集符号的原始字节。
- securityEvidence
- Evidence
用于加载程序集的证据。
返回
加载的程序集。
实现
- 属性
例外
rawAssembly
为 null
。
rawAssembly
不是当前加载的运行时的有效程序集。
在卸载的应用程序域上尝试该操作。
一个程序集或模块用两个不同的证据加载了两次。
securityEvidence
不是 null
。 未启用旧版 CAS 策略时,securityEvidence
应为 null
。
示例
下面的示例演示如何使用加载原始程序集。
若要运行此代码示例,必须提供完全限定的程序集名称。 有关如何获取完全限定程序集名称的信息,请参阅 程序集名称。
using namespace System;
using namespace System::IO;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
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.
array<Byte>^ loadFile( String^ filename )
{
FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
array<Byte>^buffer = gcnew array<Byte>((int)fs->Length);
fs->Read( buffer, 0, buffer->Length );
fs->Close();
return buffer;
}
// Creates a dynamic assembly with symbol information
// and saves them to temp.dll and temp.pdb
void EmitAssembly( AppDomain^ domain )
{
AssemblyName^ assemblyName = gcnew 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, nullptr );
ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
ilGenerator->EmitWriteLine( "MyType instantiated!" );
ilGenerator->Emit( OpCodes::Ret );
typeBuilder->CreateType();
assemblyBuilder->Save( "temp.dll" );
}
ref class Resolver
{
public:
static Assembly^ MyResolver( Object^ sender, ResolveEventArgs^ args )
{
AppDomain^ domain = dynamic_cast<AppDomain^>(sender);
// Once the files are generated, this call is
// actually no longer necessary.
EmitAssembly( domain );
array<Byte>^rawAssembly = loadFile( "temp.dll" );
array<Byte>^rawSymbolStore = loadFile( "temp.pdb" );
Assembly^ assembly = domain->Load( rawAssembly, rawSymbolStore );
return assembly;
}
};
int main()
{
AppDomain^ currentDomain = AppDomain::CurrentDomain;
InstantiateMyType( currentDomain ); // Failed!
currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Resolver::MyResolver );
InstantiateMyType( currentDomain ); // OK!
}
using System;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
class LoadRawSnippet {
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");
}
}
open System
open System.IO
open System.Reflection
open System.Reflection.Emit
let instantiateMyType (domain: AppDomain) =
try
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType")
|> ignore
with e ->
printfn $"{e.Message}"
// Loads the content of a file to a byte array.
let loadFile filename =
use fs = new FileStream(filename, FileMode.Open)
let buffer = Array.zeroCreate<byte> (int fs.Length)
fs.Read(buffer, 0, buffer.Length) |> ignore
fs.Close()
buffer
// Creates a dynamic assembly with symbol information
// and saves them to temp.dll and temp.pdb
let emitAssembly (domain: AppDomain) =
let assemblyName = AssemblyName()
assemblyName.Name <- "MyAssembly"
let assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Save)
let moduleBuilder = assemblyBuilder.DefineDynamicModule("MyModule", "temp.dll", true)
let typeBuilder = moduleBuilder.DefineType("MyType", TypeAttributes.Public)
let constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null)
let ilGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine "MyType instantiated!"
ilGenerator.Emit OpCodes.Ret
typeBuilder.CreateType() |> ignore
assemblyBuilder.Save "temp.dll"
let myResolver (sender: obj) (args: ResolveEventArgs) =
let domain = sender :?> AppDomain
// Once the files are generated, this call is
// actually no longer necessary.
emitAssembly domain
let rawAssembly = loadFile "temp.dll"
let rawSymbolStore = loadFile "temp.pdb"
domain.Load(rawAssembly, rawSymbolStore)
let currentDomain = AppDomain.CurrentDomain
instantiateMyType currentDomain // Failed!
currentDomain.add_AssemblyResolve (ResolveEventHandler myResolver)
instantiateMyType currentDomain // OK!
Imports System.IO
Imports System.Reflection
Imports System.Reflection.Emit
Module Test
Sub Main()
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
InstantiateMyType(currentDomain) ' Failed!
AddHandler currentDomain.AssemblyResolve, AddressOf MyResolver
InstantiateMyType(currentDomain) ' OK!
End Sub
Sub InstantiateMyType(domain As AppDomain)
Try
' You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType")
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub
' Loads the content of a file to a byte array.
Function loadFile(filename As String) As Byte()
Dim fs As New FileStream(filename, FileMode.Open)
Dim buffer(CInt(fs.Length - 1)) As Byte
fs.Read(buffer, 0, buffer.Length)
fs.Close()
Return buffer
End Function 'loadFile
Function MyResolver(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
Dim domain As AppDomain = DirectCast(sender, AppDomain)
' Once the files are generated, this call is
' actually no longer necessary.
EmitAssembly(domain)
Dim rawAssembly As Byte() = loadFile("temp.dll")
Dim rawSymbolStore As Byte() = loadFile("temp.pdb")
Dim myAssembly As System.Reflection.Assembly = domain.Load(rawAssembly, rawSymbolStore)
Return myAssembly
End Function 'MyResolver
' Creates a dynamic assembly with symbol information
' and saves them to temp.dll and temp.pdb
Sub EmitAssembly(domain As AppDomain)
Dim assemblyName As New AssemblyName()
assemblyName.Name = "MyAssembly"
Dim assemblyBuilder As AssemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Save)
Dim moduleBuilder As ModuleBuilder = assemblyBuilder.DefineDynamicModule("MyModule", "temp.dll", True)
Dim typeBuilder As TypeBuilder = moduleBuilder.DefineType("MyType", TypeAttributes.Public)
Dim constructorBuilder As ConstructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Nothing)
Dim ilGenerator As ILGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine("MyType instantiated!")
ilGenerator.Emit(OpCodes.Ret)
typeBuilder.CreateType()
assemblyBuilder.Save("temp.dll")
End Sub
End Module 'Test
注解
有关此方法的所有重载共有的信息,请参阅 Load(AssemblyName) 方法重载。
从.NET Framework 4 开始,使用此方法加载的程序集的信任级别与应用程序域的信任级别相同。