Export (0) Print
Expand All

AppDomain.Load Method (Byte[], Byte[])

.NET Framework 1.1

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( _
   ByVal rawAssembly() As Byte, _
   ByVal rawSymbolStore() As Byte _
) As Assembly Implements _AppDomain.Load
[C#]
public virtual Assembly Load(
 byte[] rawAssembly,
 byte[] rawSymbolStore
);
[C++]
public: virtual Assembly* Load(
 unsigned char rawAssembly __gc[],
 unsigned char rawSymbolStore __gc[]
);
[JScript]
public function Load(
   rawAssembly : Byte[],
 rawSymbolStore : Byte[]
) : Assembly;

Parameters

rawAssembly
An array of type byte that is a COFF-based image containing an emitted assembly.
rawSymbolStore
An array of type byte containing the raw bytes representing the symbols for the assembly.

Return Value

The loaded assembly.

Implements

_AppDomain.Load

Exceptions

Exception Type Condition
ArgumentNullException rawAssembly is a null reference (Nothing in Visual Basic).
BadImageFormatException rawAssembly is not a valid assembly.
AppDomainUnloadedException Operations are attempted on an unloaded application domain.
SecurityException The caller does not have the correct permissions. See the requirements section.

Remarks

For more information, see the Remarks section of the AppDomain.Load Method (AssemblyName) topic.

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] 
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 Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

AppDomain Class | AppDomain Members | System Namespace | AppDomain.Load Overload List

Show:
© 2014 Microsoft