AppDomain.Load Method
Loads an Assembly into this application domain.
Overload List
Loads an Assembly given its AssemblyName.
[Visual Basic] Overloads Public Overridable Function Load(AssemblyName) As Assembly Implements _AppDomain.Load
[C#] public virtual Assembly Load(AssemblyName);
[C++] public: virtual Assembly* Load(AssemblyName*);
[JScript] public function Load(AssemblyName) : Assembly;
Loads the Assembly with a common object file format (COFF) based image containing an emitted Assembly.
[Visual Basic] Overloads Public Overridable Function Load(Byte()) As Assembly Implements _AppDomain.Load
[C#] public virtual Assembly Load(byte[]);
[C++] public: virtual Assembly* Load(unsigned char __gc[]);
[JScript] public function Load(Byte[]) : Assembly;
Loads an Assembly given its display name.
[Visual Basic] Overloads Public Overridable Function Load(String) As Assembly Implements _AppDomain.Load
[C#] public virtual Assembly Load(string);
[C++] public: virtual Assembly* Load(String*);
[JScript] public function Load(String) : Assembly;
Loads an Assembly given its AssemblyName.
[Visual Basic] Overloads Public Overridable Function Load(AssemblyName, Evidence) As Assembly Implements _AppDomain.Load
[C#] public virtual Assembly Load(AssemblyName, Evidence);
[C++] public: virtual Assembly* Load(AssemblyName*, Evidence*);
[JScript] public function Load(AssemblyName, Evidence) : Assembly;
Loads the Assembly with a common object file format (COFF) based image containing an emitted Assembly. The raw bytes representing the symbols for the Assembly are also loaded.
[Visual Basic] Overloads Public Overridable Function Load(Byte(), Byte()) As Assembly Implements _AppDomain.Load
[C#] public virtual Assembly Load(byte[], byte[]);
[C++] public: virtual Assembly* Load(unsigned char __gc[], unsigned char __gc[]);
[JScript] public function Load(Byte[], Byte[]) : Assembly;
Loads an Assembly given its display name.
[Visual Basic] Overloads Public Overridable Function Load(String, Evidence) As Assembly Implements _AppDomain.Load
[C#] public virtual Assembly Load(string, Evidence);
[C++] public: virtual Assembly* Load(String*, Evidence*);
[JScript] public function Load(String, Evidence) : Assembly;
Loads the Assembly with a common object file format (COFF) based image containing an emitted Assembly. The raw bytes representing the symbols for the Assembly are also loaded.
[Visual Basic] Overloads Public Overridable Function Load(Byte(), Byte(), Evidence) As Assembly Implements _AppDomain.Load
[C#] public virtual Assembly Load(byte[], byte[], Evidence);
[C++] public: virtual Assembly* Load(unsigned char __gc[], unsigned char __gc[], Evidence*);
[JScript] public function Load(Byte[], Byte[], Evidence) : Assembly;
Example
[Visual Basic, C#, C++] The following sample demonstrates the use of loading a raw assembly.
[Visual Basic, C#, C++] For this code example to run, you must provide the fully qualified assembly name. For information about how to obtain the fully qualified assembly name, see Assembly Names.
[Visual Basic, C#, C++] Note This example shows how to use one of the overloaded versions of Load. For other examples that might be available, see the individual overload topics.
[Visual Basic] Imports System 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 'Main 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 'InstantiateMyType ' 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)) 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 'EmitAssembly End Module 'Test [C#] 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"); } } [C++] #using <mscorlib.dll> 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(S"Assembly text name, Version, Culture, PublicKeyToken", S"MyType"); } catch (Exception* e) { Console::WriteLine(e->Message); } } // Loads the content of a file to a Byte array. 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; } // Creates a dynamic assembly with symbol information // and saves them to temp.dll and temp.pdb void EmitAssembly(AppDomain* domain) { AssemblyName* assemblyName = new AssemblyName(); assemblyName->Name = S"MyAssembly"; AssemblyBuilder* assemblyBuilder = domain->DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess::Save); ModuleBuilder* moduleBuilder = assemblyBuilder->DefineDynamicModule(S"MyModule", S"temp.dll", true); TypeBuilder* typeBuilder = moduleBuilder->DefineType(S"MyType", TypeAttributes::Public); ConstructorBuilder* constructorBuilder = typeBuilder->DefineConstructor(MethodAttributes::Public, CallingConventions::Standard, 0); ILGenerator* ilGenerator = constructorBuilder->GetILGenerator(); ilGenerator->EmitWriteLine(S"MyType instantiated!"); ilGenerator->Emit(OpCodes::Ret); typeBuilder->CreateType(); assemblyBuilder->Save(S"temp.dll"); } __gc 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); Byte rawAssembly[] = loadFile(S"temp.dll"); Byte rawSymbolStore[] = loadFile(S"temp.pdb"); Assembly* assembly = domain->Load(rawAssembly, rawSymbolStore); return assembly; } }; int main() { AppDomain* currentDomain = AppDomain::CurrentDomain; InstantiateMyType(currentDomain); // Failed! currentDomain->AssemblyResolve += new ResolveEventHandler(0, Resolver::MyResolver); InstantiateMyType(currentDomain); // OK! }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.