次の方法で共有


AppDomain.SetDynamicBase メソッド

動的に生成されたファイルが格納され、そのファイルへのアクセス先となる場所として、ディレクトリ パスを設定します。

Public Sub SetDynamicBase( _
   ByVal path As String _)
[C#]
public void SetDynamicBase(stringpath);
[C++]
public: void SetDynamicBase(String* path);
[JScript]
public function SetDynamicBase(
   path : String);

パラメータ

  • path
    動的アセンブリの格納先となる絶対パス。

例外

例外の種類 条件
AppDomainUnloadedException 操作が、アンロードされたアプリケーション ドメインで試行されています。
SecurityException 呼び出し元に、正しいアクセス許可がありません。要件のセクションを参照してください。

解説

このメソッドは、現在のインスタンスに関連付けられている内部 AppDomainSetupDynamicBase プロパティを設定します。

使用例

 
Imports System
Imports System.Reflection
Imports System.Reflection.Emit



Class ADDynamicBase
   
   ' SetDynamicBase.exe
   Overloads Shared Sub Main(args() As String)
      ' Create a new AppDomain.
      Dim setup As New AppDomainSetup()
      ' Need to set the application name before setting the dynamic base.
      setup.ApplicationName = "MyApplication"
      Dim domain As AppDomain = AppDomain.CreateDomain("MyDomain", Nothing, setup)
      
      ' Tell the domain to search for assemblies in DynamicAssemblyDir.
      domain.SetDynamicBase("C:\DynamicAssemblyDir")
      
      ' Note that the actual dynamic directory has the form
      ' <DynamicBase>\<number>\<ApplicationName>, rather than
      ' simply <DynamicBase>.
      Dim dynamicDir As [String] = domain.DynamicDirectory
      ' The AssemblyBuilder won't create this directory automatically.
      If Not System.IO.Directory.Exists(dynamicDir) Then
         System.IO.Directory.CreateDirectory(dynamicDir)
      End If
      
      ' Define the dynamic assembly.
      Dim asmName As New AssemblyName()
      asmName.Name = "DynamicHelloWorld"
      Dim asm As AssemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.Save, dynamicDir)
      
      ' Define a dynamic module in the assembly.
      Dim [mod] As ModuleBuilder
      [mod] = asm.DefineDynamicModule("DynamicHelloWorld", "DynamicHelloWorld.dll")
      
      ' Define the "HelloWorld" type in the module.
      Dim typ As TypeBuilder = [mod].DefineType("HelloWorld", TypeAttributes.Public)
      
      ' Define the "SayHello" method.
      Dim meth As MethodBuilder = typ.DefineMethod("SayHello", MethodAttributes.Public, Nothing, Nothing)
      Dim il As ILGenerator = meth.GetILGenerator()
      il.EmitWriteLine("Hello World!")
      il.Emit(OpCodes.Ret)
      
      ' Complete the HelloWorld type.
      typ.CreateType()
      
      ' Save the assembly to the dynamic assembly directory.
      asm.Save("DynamicHelloWorld.dll")
      
      ' Launch MyExecutable.exe, which will load DynamicHelloWorld.dll.
      domain.ExecuteAssembly("MyExecutable.exe")
   End Sub 'Main
End Class 'ADDynamicBase

[C#] 
using System;
using System.Reflection;
using System.Reflection.Emit;

namespace AppDomainSnippets
{
    class ADDynamicBase
    {
        // SetDynamicBase.exe
        static void Main(string[] args)
        {
            // Create a new AppDomain.
            AppDomainSetup setup = new AppDomainSetup();
            // Need to set the application name before setting the dynamic base.
            setup.ApplicationName = "MyApplication";
            AppDomain domain = AppDomain.CreateDomain("MyDomain", null, setup);

            // Tell the domain to search for assemblies in DynamicAssemblyDir.
            domain.SetDynamicBase("C:\\DynamicAssemblyDir");
            
            // Note that the actual dynamic directory has the form
            // <DynamicBase>\<number>\<ApplicationName>, rather than
            // simply <DynamicBase>.
            String dynamicDir = domain.DynamicDirectory;
            // The AssemblyBuilder won't create this directory automatically.
            if(!System.IO.Directory.Exists(dynamicDir))
            {
                System.IO.Directory.CreateDirectory(dynamicDir);
            }

            // Define the dynamic assembly.
            AssemblyName asmName = new AssemblyName();
            asmName.Name = "DynamicHelloWorld";
            AssemblyBuilder asm = AppDomain.CurrentDomain.DefineDynamicAssembly
                (asmName, AssemblyBuilderAccess.Save, dynamicDir);

            // Define a dynamic module in the assembly.
            ModuleBuilder mod;
            mod = asm.DefineDynamicModule
                ("DynamicHelloWorld", "DynamicHelloWorld.dll");

            // Define the "HelloWorld" type in the module.
            TypeBuilder typ = mod.DefineType
                ("HelloWorld", TypeAttributes.Public);

            // Define the "SayHello" method.
            MethodBuilder meth = typ.DefineMethod
                ("SayHello", MethodAttributes.Public, null, null);
            ILGenerator il = meth.GetILGenerator();
            il.EmitWriteLine("Hello World!");
            il.Emit(OpCodes.Ret);

            // Complete the HelloWorld type.
            typ.CreateType();
            
            // Save the assembly to the dynamic assembly directory.
            asm.Save("DynamicHelloWorld.dll");

            // Launch MyExecutable.exe, which will load DynamicHelloWorld.dll.
            domain.ExecuteAssembly("MyExecutable.exe");
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;

// SetDynamicBase.exe
int main() {
    String* args[] = Environment::GetCommandLineArgs();
    // Create a new AppDomain.
    AppDomainSetup* setup = new AppDomainSetup();
    // Need to set the application name before setting the dynamic base.
    setup->ApplicationName = S"MyApplication";
    AppDomain*  domain = AppDomain::CreateDomain(S"MyDomain", 0, setup);

    // Tell the domain to search for assemblies in DynamicAssemblyDir.
    domain->SetDynamicBase(S"C:\\DynamicAssemblyDir");

    // Note that the actual dynamic directory has the form
    // <DynamicBase>\<number>\<ApplicationName>, rather than
    // simply <DynamicBase>.
    String*  dynamicDir = domain->DynamicDirectory;
    // The AssemblyBuilder won't create this directory automatically.
    if (!System::IO::Directory::Exists(dynamicDir)) {
        System::IO::Directory::CreateDirectory(dynamicDir);
    }

    // Define the dynamic assembly.
    AssemblyName* asmName = new AssemblyName();
    asmName->Name = S"DynamicHelloWorld";
    AssemblyBuilder*  asmb = AppDomain::CurrentDomain->DefineDynamicAssembly
        (asmName, AssemblyBuilderAccess::Save, dynamicDir);

    // Define a dynamic module in the assembly.
    ModuleBuilder* mod;
    mod = asmb->DefineDynamicModule
        (S"DynamicHelloWorld", S"DynamicHelloWorld.dll");

    // Define the S"HelloWorld" type in the module.
    TypeBuilder*  typ = mod->DefineType
        (S"HelloWorld", TypeAttributes::Public);

    // Define the S"SayHello" method.
    MethodBuilder*  meth = typ->DefineMethod
        (S"SayHello", MethodAttributes::Public, 0, 0);
    ILGenerator*  il = meth->GetILGenerator();
    il->EmitWriteLine(S"Hello World!");
    il->Emit(OpCodes::Ret);

    // Complete the HelloWorld type.
    typ->CreateType();

    // Save the assembly to the dynamic assembly directory.
    asmb->Save(S"DynamicHelloWorld.dll");

    // Launch MyExecutable.exe, which will load DynamicHelloWorld.dll.
    domain->ExecuteAssembly(S"MyExecutable.exe");
}

[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 セキュリティ:

参照

AppDomain クラス | AppDomain メンバ | System 名前空間 | DynamicBase