AppDomain.Load メソッド (Byte[], Byte[])
生成された Assembly を含む COFF (Common Object File Format) ベースのイメージを使用して、 Assembly を読み込みます。 Assembly のシンボルを表す生バイトも読み込まれます。
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 charrawAssembly __gc[],unsigned charrawSymbolStore __gc[]);
[JScript]
public function Load(
rawAssembly : Byte[],rawSymbolStore : Byte[]) : Assembly;
パラメータ
- rawAssembly
生成されたアセンブリを含む COFF ベースのイメージである byte 型配列。 - rawSymbolStore
アセンブリのシンボルを表す生バイトを格納している byte 型の配列。
戻り値
読み込み済みのアセンブリ。
実装
例外
例外の種類 | 条件 |
---|---|
ArgumentNullException | rawAssembly が null 参照 (Visual Basic では Nothing) です。 |
BadImageFormatException | rawAssembly が有効なアセンブリではありません。 |
AppDomainUnloadedException | 操作が、アンロードされたアプリケーション ドメインで試行されています。 |
SecurityException | 呼び出し元に、正しいアクセス許可がありません。要件のセクションを参照してください。 |
解説
詳細については、「 AppDomain.Load メソッド (AssemblyName) 」の解説を参照してください。
使用例
[Visual Basic, C#, C++] 生アセンブリの読み込み方法を次の例に示します。
[Visual Basic, C#, C++] このコード例を実行するには、アセンブリの完全限定名を指定する必要があります。アセンブリの完全限定名を取得する方法については、「 アセンブリ名 」を参照してください。
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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
.NET Framework セキュリティ:
- ReflectionPermission (Type.InvokeMember などの機構を通じて遅延バインディングが呼び出されるときに必要なアクセス許可) ReflectionPermissionFlag.MemberAccess (関連する列挙体)
- WebPermission ("file://" 以外で始まる URI を読み取るために必要なアクセス許可)
- FileIOPermission (ファイルまたはディレクトリから読み取るためのアクセス許可、またはパス自体の情報に対するアクセス許可) FileIOPermissionAccess.Read 、 FileIOPermissionAccess.PathDiscovery (関連する列挙体)
参照
AppDomain クラス | AppDomain メンバ | System 名前空間 | AppDomain.Load オーバーロードの一覧