AppDomainSetup.DynamicBase プロパティ

定義

動的に生成されたファイルが格納されるディレクトリのベース ディレクトリを取得または設定します。Gets or sets the base directory where the directory for dynamically generated files is located.

public:
 property System::String ^ DynamicBase { System::String ^ get(); void set(System::String ^ value); };
public string DynamicBase { get; set; }
member this.DynamicBase : string with get, set
Public Property DynamicBase As String

プロパティ値

DynamicDirectory が置かれているディレクトリ。The directory where the DynamicDirectory is located.


このプロパティの戻り値は、割り当てられた値とは異なります。The return value of this property is different from the value assigned.

実装

例外

アプリケーション ドメインでのアプリケーション名が null であるため、このプロパティを設定することができません。This property cannot be set because the application name on the application domain is null.

次の例は、 DynamicBaseプロパティを使用して、動的アセンブリの読み込み時にアプリケーションドメインがプローブするパスを設定する方法を示しています。The following example demonstrates how to use the DynamicBase property to set the path an application domain probes when loading dynamic assemblies.

この例ではAppDomainSetup 、オブジェクトを作成ApplicationNameし、そのプロパティを "example DynamicBase " に設定し、そのプロパティを "c:\ dynamicassemblydir" に設定します。The example creates an AppDomainSetup object and sets its ApplicationName property to "Example" and its DynamicBase property to "C:\DynamicAssemblyDir". この例では、 DynamicBaseプロパティを表示し、アプリケーション名のハッシュコードが、最初に割り当てられたパスのサブディレクトリとして追加されたことを示しています。The example then displays the DynamicBase property, to show that the hash code of the application name has been appended as a subdirectory of the path that was originally assigned.

注意

この例の基本ディレクトリは、サンプルアプリケーションのプローブパスの外部にすることを目的としています。The base directory in this example is intended to be outside the probing path for the example application. この例は、別の場所でコンパイルしてください。Be sure to compile the example in a different location. この例を実行するたびに、ベースディレクトリとそのすべてのサブディレクトリが削除されます。Delete the base directory and all its subdirectories each time you run the example.

この例では、 AppDomainSetupオブジェクトを使用して新しいアプリケーションドメインを作成します。The example creates a new application domain using the AppDomainSetup object. この例では、動的ディレクトリがまだ存在しない場合は作成します。The example then creates the dynamic directory if it does not already exist. この例では、アプリケーションドメインのAppDomain.DynamicDirectoryプロパティを使用して動的ディレクトリの名前を取得していますが、元のパス、アプリケーション名のハッシュコード、およびを連結して、ディレクトリを事前に作成することもできます。アプリケーション名。Although the example uses the application domain's AppDomain.DynamicDirectory property to get the name of the dynamic directory, it could just as easily create the directory beforehand by concatenating the original path, the hash code of the application name, and the application name.

この例にはGenerateDynamicAssembly 、という名前DynamicHelloWorld.dllのアセンブリを出力し、新しいアプリケーションドメインの動的ディレクトリに格納するメソッドがあります。The example has a GenerateDynamicAssembly method that emits an assembly named DynamicHelloWorld.dll and stores it in the new application domain's dynamic directory. 動的アセンブリには、というHelloWorld名前HelloFromADの静的メソッド (Shared Visual Basic) を持つ型が1つ含まれています。The dynamic assembly contains one type, HelloWorld, that has a static method (Shared method in Visual Basic) named HelloFromAD. このメソッドを呼び出すと、アプリケーションドメインの名前が表示されます。Calling this method displays the name of the application domain.

クラスExampleはからMarshalByRefObject派生するため、この例では、新しいアプリケーションドメインにクラスのインスタンスを作成し、 Testそのメソッドを呼び出すことができます。The Example class derives from MarshalByRefObject, so the example can create an instance of the class in the new application domain and call its Test method. メソッドTestは、表示名を指定して動的アセンブリを読み込み、 HelloFromAD静的メソッドを呼び出します。The Test method loads the dynamic assembly by its display name and calls the static HelloFromAD method.

という名前DynamicHelloWorld.dllのアセンブリのコードを記述し、この例と同じディレクトリにコンパイルすることで、通常のプローブパスの後に動的ディレクトリが検索されることを示すことができます。You can show that the dynamic directory is searched after the normal probing paths by writing code for an assembly named DynamicHelloWorld.dll and compiling it in the same directory as this example. アセンブリには、という名前HelloWorld HelloFromADの静的メソッドを持つという名前のクラスが必要です。The assembly must have a class named HelloWorld with a static method named HelloFromAD. このメソッドの機能は、例のものと同じである必要はありません。単にコンソールに文字列を表示できます。This method does not have to have the same functionality as the one in the example; it can simply display a string to the console. アセンブリには、そのバージョンAssemblyVersionAttributeを1.0.0.0 に設定する属性も必要です。The assembly must also have an AssemblyVersionAttribute attribute that sets its version to 1.0.0.0. この例を実行すると、現在のディレクトリでコンパイルしたアセンブリが検索されてから、動的ディレクトリが検索されます。When you run the example, the assembly you compiled in the current directory is found before the dynamic directory is searched.

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

public ref class Example : MarshalByRefObject
{
public:
   void Test()
   {
      Assembly^ dynAssem = Assembly::Load(
         "DynamicHelloWorld, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");

      Type^ myType = dynAssem->GetType("HelloWorld");
      myType->InvokeMember("HelloFromAD", BindingFlags::Public | 
         BindingFlags::Static | BindingFlags::InvokeMethod, 
         Type::DefaultBinder, nullptr, nullptr);
   }
};


static void GenerateDynamicAssembly(String^ location)
{
   // Define the dynamic assembly and the module. There is only one
   // module in this assembly. Note that the call to DefineDynamicAssembly 
   // specifies the location where the assembly will be saved. The 
   // assembly version is 1.0.0.0.
   //
   AssemblyName^ asmName = gcnew AssemblyName("DynamicHelloWorld");
   asmName->Version = gcnew Version("1.0.0.0");

   AssemblyBuilder^ ab = 
      AppDomain::CurrentDomain->DefineDynamicAssembly( 
         asmName, AssemblyBuilderAccess::Save, location);

   String^ moduleName = asmName->Name + ".exe";
   ModuleBuilder^ mb = ab->DefineDynamicModule(asmName->Name, moduleName);
   
   // Define the "HelloWorld" type, with one static method.
   TypeBuilder^ tb = mb->DefineType("HelloWorld", TypeAttributes::Public);
   MethodBuilder^ hello = tb->DefineMethod("HelloFromAD", 
      MethodAttributes::Public | MethodAttributes::Static, nullptr, nullptr);

   // The method displays a message that contains the name of the application
   // domain where the method is executed.
   ILGenerator^ il = hello->GetILGenerator();
   il->Emit(OpCodes::Ldstr, "Hello from '{0}'!");
   il->Emit(OpCodes::Call, AppDomain::typeid->GetProperty("CurrentDomain")->GetGetMethod());
   il->Emit(OpCodes::Call, AppDomain::typeid->GetProperty("FriendlyName")->GetGetMethod());
   il->Emit(OpCodes::Call, Console::typeid->GetMethod("WriteLine", 
                            gcnew array<Type^> { String::typeid, String::typeid }));
   il->Emit(OpCodes::Ret);

   // Complete the HelloWorld type and save the assembly. The assembly
   // is placed in the location specified by DefineDynamicAssembly.
   Type^ myType = tb->CreateType();
   ab->Save(moduleName);
};

void main()
{
   // Prepare to create a new application domain.
   AppDomainSetup^ setup = gcnew AppDomainSetup();

   // Set the application name before setting the dynamic base.
   setup->ApplicationName = "Example";
   
   // Set the location of the base directory where assembly resolution 
   // probes for dynamic assemblies. Note that the hash code of the 
   // application name is concatenated to the base directory name you 
   // supply. 
   setup->DynamicBase = "C:\\DynamicAssemblyDir";
   Console::WriteLine("DynamicBase is set to '{0}'.", setup->DynamicBase);

   AppDomain^ ad = AppDomain::CreateDomain("MyDomain", nullptr, setup);
   
   // The dynamic directory name is the dynamic base concatenated with
   // the application name: <DynamicBase>\<hash code>\<ApplicationName>
   String^ dynamicDir = ad->DynamicDirectory;
   Console::WriteLine("Dynamic directory is '{0}'.", dynamicDir);

   // The AssemblyBuilder won't create this directory automatically.
   if (!System::IO::Directory::Exists(dynamicDir))
   {
      Console::WriteLine("Creating the dynamic directory.");
      System::IO::Directory::CreateDirectory(dynamicDir);
   }

   // Generate a dynamic assembly and store it in the dynamic 
   // directory.
   GenerateDynamicAssembly(dynamicDir);

   // Create an instance of the Example class in the application domain,
   // and call its Test method to load the dynamic assembly and use it.
   Example^ ex = (Example^) ad->CreateInstanceAndUnwrap( 
         Example::typeid->Assembly->FullName, "Example");
   ex->Test();
}

/* This example produces output similar to the following:

DynamicBase is set to 'C:\DynamicAssemblyDir\5e4a7545'.
Dynamic directory is 'C:\DynamicAssemblyDir\5e4a7545\Example'.
Creating the dynamic directory.
Hello from 'MyDomain'!
 */
using System;
using System.Reflection;
using System.Reflection.Emit;

public class Example : MarshalByRefObject
{
   static void Main()
   {
      // Prepare to create a new application domain.
      AppDomainSetup setup = new AppDomainSetup();
   
      // Set the application name before setting the dynamic base.
      setup.ApplicationName = "Example";
   
      // Set the location of the base directory where assembly resolution 
      // probes for dynamic assemblies. Note that the hash code of the 
      // application name is concatenated to the base directory name you 
      // supply. 
      setup.DynamicBase = "C:\\DynamicAssemblyDir";
      Console.WriteLine("DynamicBase is set to '{0}'.", setup.DynamicBase);

      AppDomain ad = AppDomain.CreateDomain("MyDomain", null, setup);
   
      // The dynamic directory name is the dynamic base concatenated with
      // the application name: <DynamicBase>\<hash code>\<ApplicationName>
      string dynamicDir = ad.DynamicDirectory;
      Console.WriteLine("Dynamic directory is '{0}'.", dynamicDir);

      // The AssemblyBuilder won't create this directory automatically.
      if (!System.IO.Directory.Exists(dynamicDir))
      {
         Console.WriteLine("Creating the dynamic directory.");
         System.IO.Directory.CreateDirectory(dynamicDir);
      }

      // Generate a dynamic assembly and store it in the dynamic 
      // directory.
      GenerateDynamicAssembly(dynamicDir);

      // Create an instance of the Example class in the application domain,
      // and call its Test method to load the dynamic assembly and use it.
      Example ex = (Example) ad.CreateInstanceAndUnwrap(
         typeof(Example).Assembly.FullName, "Example");
      ex.Test();
   }

   public void Test()
   {
      Assembly dynAssem = Assembly.Load(
         "DynamicHelloWorld, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");

      Type myType = dynAssem.GetType("HelloWorld");
      myType.InvokeMember("HelloFromAD", BindingFlags.Public | 
         BindingFlags.Static | BindingFlags.InvokeMethod, 
         Type.DefaultBinder, null, null);
   }


   private static void GenerateDynamicAssembly(string location)
   {
      // Define the dynamic assembly and the module. There is only one
      // module in this assembly. Note that the call to DefineDynamicAssembly 
      // specifies the location where the assembly will be saved. The 
      // assembly version is 1.0.0.0.
      //
      AssemblyName asmName = new AssemblyName("DynamicHelloWorld");
      asmName.Version = new Version("1.0.0.0");

      AssemblyBuilder ab = 
         AppDomain.CurrentDomain.DefineDynamicAssembly( 
            asmName, AssemblyBuilderAccess.Save, location);

      String moduleName = asmName.Name + ".exe";
      ModuleBuilder mb = ab.DefineDynamicModule(asmName.Name, moduleName);
      
      // Define the "HelloWorld" type, with one static method.
      TypeBuilder tb = mb.DefineType("HelloWorld", TypeAttributes.Public);
      MethodBuilder hello = tb.DefineMethod("HelloFromAD", 
         MethodAttributes.Public | MethodAttributes.Static, null, null);

      // The method displays a message that contains the name of the application
      // domain where the method is executed.
      ILGenerator il = hello.GetILGenerator();
      il.Emit(OpCodes.Ldstr, "Hello from '{0}'!");
      il.Emit(OpCodes.Call, typeof(AppDomain).GetProperty("CurrentDomain").GetGetMethod());
      il.Emit(OpCodes.Call, typeof(AppDomain).GetProperty("FriendlyName").GetGetMethod());
      il.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", 
                             new Type[] { typeof(String), typeof(String) }));
      il.Emit(OpCodes.Ret);

      // Complete the HelloWorld type and save the assembly. The assembly
      // is placed in the location specified by DefineDynamicAssembly.
      Type myType = tb.CreateType();
      ab.Save(moduleName);
   }
}

/* This example produces output similar to the following:

DynamicBase is set to 'C:\DynamicAssemblyDir\5e4a7545'.
Dynamic directory is 'C:\DynamicAssemblyDir\5e4a7545\Example'.
Creating the dynamic directory.
Hello from 'MyDomain'!
 */
Imports System.Reflection
Imports System.Reflection.Emit

Public Class Example 
   Inherits MarshalByRefObject
   
   Shared Sub Main(args() As String)

      ' Prepare to create a new application domain.
      Dim setup As New AppDomainSetup()

      ' Set the application name before setting the dynamic base.
      setup.ApplicationName = "Example"
      
      ' Set the location of the base directory where assembly resolution 
      ' probes for dynamic assemblies. Note that the hash code of the 
      ' application name is concatenated to the base directory name you 
      ' supply. 
      setup.DynamicBase = "C:\DynamicAssemblyDir"
      Console.WriteLine("DynamicBase is set to '{0}'.", setup.DynamicBase)

      Dim ad As AppDomain = AppDomain.CreateDomain("MyDomain", Nothing, setup)
      
      ' The dynamic directory name is the dynamic base concatenated with
      ' the application name: <DynamicBase>\<hash code>\<ApplicationName>
      Dim dynamicDir As String = ad.DynamicDirectory 
      Console.WriteLine("Dynamic directory is '{0}'.", dynamicDir)

      ' The AssemblyBuilder won't create this directory automatically.
      If Not System.IO.Directory.Exists(dynamicDir) Then 
         Console.WriteLine("Creating the dynamic directory.")
         System.IO.Directory.CreateDirectory(dynamicDir)
      End If

      ' Generate a dynamic assembly and store it in the dynamic 
      ' directory.
      GenerateDynamicAssembly(dynamicDir) 

      ' Create an instance of the Example class in the application domain,
      ' and call its Test method to load the dynamic assembly and use it.  
      Dim ex As Example = CType( _
         ad.CreateInstanceAndUnwrap( _
            GetType(Example).Assembly.FullName, "Example"), Example)
      ex.Test()
   End Sub

   Public Sub Test() 

      Dim dynAssem As [Assembly] = Assembly.Load(
         "DynamicHelloWorld, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")

      Dim myType As Type = dynAssem.GetType("HelloWorld")
      myType.InvokeMember("HelloFromAD", BindingFlags.Public Or _
         BindingFlags.Static Or BindingFlags.InvokeMethod, _
         Type.DefaultBinder, Nothing, Nothing) 'New Object() {})
   End Sub


   Private Shared Sub GenerateDynamicAssembly(ByVal location As String)
      
      ' Define the dynamic assembly and the module. There is only one
      ' module in this assembly. Note that the call to DefineDynamicAssembly 
      ' specifies the location where the assembly will be saved. The 
      ' assembly version is 1.0.0.0.
      '
      Dim asmName As New AssemblyName("DynamicHelloWorld")
      asmName.Version = New Version("1.0.0.0")

      Dim ab As AssemblyBuilder = _
         AppDomain.CurrentDomain.DefineDynamicAssembly( _
            asmName, AssemblyBuilderAccess.Save, location)

      Dim moduleName As String = asmName.Name & ".dll"
      Dim mb As ModuleBuilder = ab.DefineDynamicModule(asmName.Name, moduleName)
      
      ' Define the "HelloWorld" type, with one static method.
      Dim tb As TypeBuilder = mb.DefineType("HelloWorld", TypeAttributes.Public)
      Dim hello As MethodBuilder = tb.DefineMethod("HelloFromAD", _
         MethodAttributes.Public Or MethodAttributes.Static, Nothing, Nothing)

      ' The method displays a message that contains the name of the application
      ' domain where the method is executed.
      Dim il As ILGenerator = hello.GetILGenerator()
      il.Emit(OpCodes.Ldstr, "Hello from '{0}'!")
      il.Emit(OpCodes.Call, GetType(AppDomain).GetProperty("CurrentDomain").GetGetMethod())
      il.Emit(OpCodes.Call, GetType(AppDomain).GetProperty("FriendlyName").GetGetMethod())
      il.Emit(OpCodes.Call, GetType(Console).GetMethod("WriteLine", _
                             New Type() { GetType(String), GetType(String) }))
      il.Emit(OpCodes.Ret)

      ' Complete the HelloWorld type and save the assembly. The assembly
      ' is placed in the location specified by DefineDynamicAssembly.
      Dim myType As Type = tb.CreateType()
      ab.Save(moduleName)
   End Sub
End Class 

' This example produces output similar to the following:
'
'DynamicBase is set to 'C:\DynamicAssemblyDir\5e4a7545'.
'Dynamic directory is 'C:\DynamicAssemblyDir\5e4a7545\Example'.
'Creating the dynamic directory.
'Hello from 'MyDomain'!

注釈

このプロパティを使用して、新しいアプリケーションドメインの動的ディレクトリの配置先となるベースディレクトリを設定します。Use this property to set the base directory where the dynamic directory for the new application domain will be located. 新しいアプリケーションドメインのコードによってアセンブリが読み込まれると、アセンブリの解決は最初に通常のプローブパスで開始されます。When code in the new application domain loads an assembly, assembly resolution looks first in the normal probing paths. アセンブリが見つからない場合は、 AppDomain.DynamicDirectoryプロパティによって返される動的ディレクトリを検索します。If it does not find the assembly, it looks in the dynamic directory, which is returned by the AppDomain.DynamicDirectory property. 新しいアプリケーションドメインによって読み込まれて実行される動的アセンブリは、そこに配置できます。Dynamic assemblies that will be loaded and executed by the new application domain can be placed there.

DynamicBaseプロパティにパスを割り当てると、追加のサブディレクトリが追加されます。このサブディレクトリの名前は、 ApplicationNameプロパティに割り当てられている値のハッシュコードです。When you assign a path to the DynamicBase property, an additional subdirectory is added; the name of this subdirectory is the hash code of the value assigned to the ApplicationName property. したがって、このプロパティによって後から返される基本ディレクトリは、割り当てられた値とは常に異なります。Thus, the base directory subsequently returned by this property is always different from the value assigned.

重要

このプロパティに値を割り当てると、ディレクトリは作成されません。Assigning a value to this property does not create any directories. ディレクトリは、それらを使用するコードによって作成または検証される必要があります。The directories must be created or verified by the code that uses them.

動的ディレクトリは、のDynamicBaseサブディレクトリです。The dynamic directory is a subdirectory of DynamicBase. 単純な名前はApplicationName 、プロパティによって返される値です。そのため、その形式は元のパス\ハッシュコード\のアプリケーション名になります。Its simple name is the value returned by the ApplicationName property, so its format is original path\hash code\application name.

セキュリティ

FileIOPermission
パス情報にアクセスします。for access to the path information. 関連付けられた列挙型:PathDiscoveryAssociated enumeration: PathDiscovery

適用対象