AppDomain.Load Yöntem
Tanım
Önemli
Bazı bilgiler ürünün ön sürümüyle ilgilidir ve sürüm öncesinde önemli değişiklikler yapılmış olabilir. Burada verilen bilgilerle ilgili olarak Microsoft açık veya zımni hiçbir garanti vermez.
Bu uygulama etki alanına bir Assembly yükler.
Aşırı Yüklemeler
Load(Byte[]) |
öğesini, Assembly yayılan öğesini içeren ortak nesne dosyası biçimi (COFF) tabanlı bir Assemblygörüntüyle yükler. |
Load(AssemblyName) |
Belirli bir Assembly öğesini AssemblyNameyükler. |
Load(String) |
Verilen görünen Assembly adını yükler. |
Load(Byte[], Byte[]) |
öğesini, Assembly yayılan öğesini içeren ortak nesne dosyası biçimi (COFF) tabanlı bir Assemblygörüntüyle yükler. için Assembly sembolleri temsil eden ham baytlar da yüklenir. |
Load(AssemblyName, Evidence) |
Geçersiz.
Belirli bir Assembly öğesini AssemblyNameyükler. |
Load(String, Evidence) |
Geçersiz.
Verilen görünen Assembly adını yükler. |
Load(Byte[], Byte[], Evidence) |
Geçersiz.
öğesini, Assembly yayılan öğesini içeren ortak nesne dosyası biçimi (COFF) tabanlı bir Assemblygörüntüyle yükler. için Assembly sembolleri temsil eden ham baytlar da yüklenir. |
Load(Byte[])
- Kaynak:
- AppDomain.cs
- Kaynak:
- AppDomain.cs
- Kaynak:
- 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
Parametreler
- rawAssembly
- Byte[]
Yayılan bir derleme içeren COFF tabanlı bir görüntü olan tür byte
dizisi.
Döndürülenler
Yüklenen derleme.
Uygulamalar
Özel durumlar
rawAssembly
, null
değeridir.
rawAssembly
şu anda yüklü olan çalışma zamanı için geçerli bir derleme değil.
İşlem, yüklenmemiş bir uygulama etki alanında deneniyor.
Bir derleme veya modül iki farklı kanıtla iki kez yüklendi.
Örnekler
Aşağıdaki örnekte ham derleme yükleme işleminin kullanımı gösterilmektedir.
Bu kod örneğinin çalışması için, tam derleme adını sağlamanız gerekir. Tam derleme adını alma hakkında bilgi için bkz. Derleme Adları.
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
Açıklamalar
Bu yöntemin tüm aşırı yüklemelerinde ortak olan bilgiler için bkz Load(AssemblyName) . yöntem aşırı yüklemesi.
.NET Framework 4'ünden başlayarak, bu yöntem kullanılarak yüklenen bir derlemenin güven düzeyi, uygulama etki alanının güven düzeyiyle aynıdır.
Şunlara uygulanır
Load(AssemblyName)
- Kaynak:
- AppDomain.cs
- Kaynak:
- AppDomain.cs
- Kaynak:
- AppDomain.cs
Belirli bir Assembly öğesini AssemblyNameyükler.
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
Parametreler
- assemblyRef
- AssemblyName
Yüklenip yüklenmeyecek derlemeyi açıklayan bir nesne.
Döndürülenler
Yüklenen derleme.
Uygulamalar
Özel durumlar
assemblyRef
, null
değeridir.
assemblyRef
bulunamadı.
assemblyRef
şu anda yüklü olan çalışma zamanı için geçerli bir derleme değil.
İşlem, yüklenmemiş bir uygulama etki alanında deneniyor.
Bir derleme veya modül iki farklı kanıtla iki kez yüklendi.
Açıklamalar
Bu yöntem yalnızca bir derlemeyi geçerli uygulama etki alanına yüklemek için kullanılmalıdır. Bu yöntem, statik Assembly.Load yöntemi çağıramayan birlikte çalışabilirlik çağıranları için kolaylık olarak sağlanır. Derlemeleri diğer uygulama etki alanlarına yüklemek için gibi CreateInstanceAndUnwrapbir yöntem kullanın.
İstenen derlemenin bir sürümü zaten yüklüyse, farklı bir sürüm istense bile bu yöntem yüklenen derlemeyi döndürür.
için assemblyRef
kısmi derleme adı kullanılması önerilmez. (Kısmi ad bir veya daha fazla kültür, sürüm veya ortak anahtar belirtecini atlar. Nesne yerine AssemblyName dizeyi kullanan aşırı yüklemeler için, "MyAssembly, Version=1.0.0.0" kısmi bir ad örneğidir ve "MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=18ab3442da84b47" tam ad örneğidir.) Kısmi adların kullanılması performans üzerinde olumsuz bir etkiye sahiptir. Buna ek olarak, kısmi derleme adı yalnızca uygulama temel dizininde (BaseDirectory veya AppDomainSetup.ApplicationBase) derlemenin tam bir kopyası varsa, bir derlemeyi genel derleme önbelleğinden yükleyebilir.
Geçerli AppDomain nesne uygulama etki alanını A
temsil ediyorsa ve Load yöntemi uygulama etki alanından B
çağrılırsa, derleme her iki uygulama etki alanına da yüklenir. Örneğin, aşağıdaki kod yeni uygulama etki alanına ChildDomain
ve ayrıca kodun yürütüldüğü uygulama etki alanına yüklenirMyAssembly
:
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")
Derleme, 'den türetilmediğinden ve bu nedenle yöntemin dönüş değeri Load sıralanamadığından MarshalByRefObjecther iki etki alanına Assembly da yüklenir. Bunun yerine, ortak dil çalışma zamanı derlemeyi çağıran uygulama etki alanına yüklemeye çalışır. İki uygulama etki alanına yüklenen derlemeler, iki uygulama etki alanı için yol ayarları farklıysa farklı olabilir.
Not
Hem özellik hem AssemblyName.CodeBase de AssemblyName.Name özellik ayarlanırsa, derlemeyi yüklemeye yönelik ilk denemede görünen ad (özellik tarafından Assembly.FullName döndürülen sürüm, kültür vb.) kullanılır. Dosya bulunamazsa, CodeBase derlemeyi aramak için özelliği kullanılır. Derleme kullanılarak CodeBasebulunursa, görünen ad derlemeyle eşleştirilir. Eşleşme başarısız olursa, bir FileLoadException oluşturulur.
Şunlara uygulanır
Load(String)
- Kaynak:
- AppDomain.cs
- Kaynak:
- AppDomain.cs
- Kaynak:
- AppDomain.cs
Verilen görünen Assembly adını yükler.
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
Parametreler
Döndürülenler
Yüklenen derleme.
Uygulamalar
Özel durumlar
assemblyString
null
assemblyString
bulunamadı.
assemblyString
şu anda yüklü olan çalışma zamanı için geçerli bir derleme değil.
İşlem, yüklenmemiş bir uygulama etki alanında deneniyor.
Bir derleme veya modül iki farklı kanıtla iki kez yüklendi.
Açıklamalar
Bu yöntemin tüm aşırı yüklemelerinde ortak olan bilgiler için bkz Load(AssemblyName) . yöntem aşırı yüklemesi.
Şunlara uygulanır
Load(Byte[], Byte[])
- Kaynak:
- AppDomain.cs
- Kaynak:
- AppDomain.cs
- Kaynak:
- 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
Parametreler
- rawAssembly
- Byte[]
Yayılan bir derleme içeren COFF tabanlı bir görüntü olan tür byte
dizisi.
- rawSymbolStore
- Byte[]
Derlemenin simgelerini temsil eden ham baytları içeren tür byte
dizisi.
Döndürülenler
Yüklenen derleme.
Uygulamalar
Özel durumlar
rawAssembly
, null
değeridir.
rawAssembly
şu anda yüklü olan çalışma zamanı için geçerli bir derleme değil.
İşlem, yüklenmemiş bir uygulama etki alanında deneniyor.
Bir derleme veya modül iki farklı kanıtla iki kez yüklendi.
Örnekler
Aşağıdaki örnekte ham derleme yükleme işleminin kullanımı gösterilmektedir.
Bu kod örneğinin çalışması için, tam derleme adını sağlamanız gerekir. Tam derleme adını alma hakkında bilgi için bkz. Derleme Adları.
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
Açıklamalar
Bu yöntemin tüm aşırı yüklemelerinde ortak olan bilgiler için bkz Load(AssemblyName) . yöntem aşırı yüklemesi.
.NET Framework 4'ünden başlayarak, bu yöntem kullanılarak yüklenen bir derlemenin güven düzeyi, uygulama etki alanının güven düzeyiyle aynıdır.
Şunlara uygulanır
Load(AssemblyName, Evidence)
Dikkat
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.
Belirli bir Assembly öğesini AssemblyNameyükler.
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
Parametreler
- assemblyRef
- AssemblyName
Yüklenip yüklenmeyecek derlemeyi açıklayan bir nesne.
- assemblySecurity
- Evidence
Derleme yüklemesi için kanıt.
Döndürülenler
Yüklenen derleme.
Uygulamalar
- Öznitelikler
Özel durumlar
assemblyRef
null
assemblyRef
bulunamadı.
assemblyRef
şu anda yüklü olan çalışma zamanı için geçerli bir derleme değil.
İşlem, yüklenmemiş bir uygulama etki alanında deneniyor.
Bir derleme veya modül iki farklı kanıtla iki kez yüklendi.
Açıklamalar
Bu yöntemin tüm aşırı yüklemelerinde ortak olan bilgiler için bkz Load(AssemblyName) . yöntem aşırı yüklemesi.
Şunlara uygulanır
Load(String, Evidence)
Dikkat
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.
Verilen görünen Assembly adını yükler.
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
Parametreler
- assemblySecurity
- Evidence
Derleme yüklemesi için kanıt.
Döndürülenler
Yüklenen derleme.
Uygulamalar
- Öznitelikler
Özel durumlar
assemblyString
null
assemblyString
bulunamadı.
assemblyString
şu anda yüklü olan çalışma zamanı için geçerli bir derleme değil.
İşlem, yüklenmemiş bir uygulama etki alanında deneniyor.
Bir derleme veya modül iki farklı kanıtla iki kez yüklendi.
Açıklamalar
Bu yöntemin tüm aşırı yüklemelerinde ortak olan bilgiler için bkz Load(AssemblyName) . yöntem aşırı yüklemesi.
Şunlara uygulanır
Load(Byte[], Byte[], Evidence)
Dikkat
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
Parametreler
- rawAssembly
- Byte[]
Yayılan bir derleme içeren COFF tabanlı bir görüntü olan tür byte
dizisi.
- rawSymbolStore
- Byte[]
Derlemenin simgelerini temsil eden ham baytları içeren tür byte
dizisi.
- securityEvidence
- Evidence
Derleme yüklemesi için kanıt.
Döndürülenler
Yüklenen derleme.
Uygulamalar
- Öznitelikler
Özel durumlar
rawAssembly
, null
değeridir.
rawAssembly
şu anda yüklü olan çalışma zamanı için geçerli bir derleme değil.
İşlem, yüklenmemiş bir uygulama etki alanında deneniyor.
Bir derleme veya modül iki farklı kanıtla iki kez yüklendi.
securityEvidence
değil null
. Eski CAS ilkesi etkinleştirilmediğinde olmalıdır securityEvidence
null
.
Örnekler
Aşağıdaki örnekte ham derleme yükleme işleminin kullanımı gösterilmektedir.
Bu kod örneğinin çalışması için, tam derleme adını sağlamanız gerekir. Tam derleme adını alma hakkında bilgi için bkz. Derleme Adları.
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
Açıklamalar
Bu yöntemin tüm aşırı yüklemelerinde ortak olan bilgiler için bkz Load(AssemblyName) . yöntem aşırı yüklemesi.
.NET Framework 4'ünden başlayarak, bu yöntem kullanılarak yüklenen bir derlemenin güven düzeyi, uygulama etki alanının güven düzeyiyle aynıdır.