AppDomain.DynamicDirectory プロパティ

定義

動的に作成されたアセンブリを探すためにアセンブリ リゾルバーが使用するディレクトリを取得します。Gets the directory that the assembly resolver uses to probe for dynamically created assemblies.

public:
 property System::String ^ DynamicDirectory { System::String ^ get(); };
public string DynamicDirectory { get; }
member this.DynamicDirectory : string
Public ReadOnly Property DynamicDirectory As String

プロパティ値

動的に作成されたアセンブリを探すためにアセンブリ リゾルバーが使用するディレクトリ。The directory that the assembly resolver uses to probe for dynamically created assemblies.

実装

例外

アンロードされたアプリケーション ドメインで操作しようとします。The operation is attempted on an unloaded application domain.

次の例では、動的アセンブリのディレクトリを使用してアプリケーションドメインを作成し、動的アセンブリを出力して、動的ディレクトリに格納します。その後、アセンブリを新しいアプリケーションドメインに読み込み、それを使用します。The following example creates an application domain with a directory for dynamic assemblies, emits a dynamic assembly and stores it in the dynamic directory, and then loads the assembly into the new application domain and uses it.

この例では、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. この例では、DynamicDirectory プロパティを使用してディレクトリの名前を取得し、ディレクトリを作成できるようにします。The example uses the DynamicDirectory property to retrieve the name of the directory, so it can create the directory. (この例では、元のパス、アプリケーション名のハッシュコード、アプリケーション名を連結することで、ディレクトリを事前に簡単に作成できます)。(The example could just as easily create the directory beforehand by concatenating the original path, the hash code of the application name, and the application name.)

この例には、DynamicHelloWorld.dll という名前のアセンブリを出力し、新しいアプリケーションドメインの動的ディレクトリに格納する GenerateDynamicAssembly メソッドがあります。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という1つの型が含まれています。この型には HelloFromADという名前の静的メソッド (Visual Basic でShared メソッド) があります。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. アセンブリには、HelloFromADという名前の静的メソッドを持つ HelloWorld という名前のクラスが必要です。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. アセンブリには、そのバージョンを1.0.0.0 に設定する AssemblyVersionAttribute 属性も必要です。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'!

注釈

動的ディレクトリを設定するには、新しいアプリケーションドメインの作成に使用される AppDomainSetup オブジェクトの AppDomainSetup.DynamicBase プロパティに、ベースディレクトリパスを割り当てます。To set the dynamic directory, assign a base directory path to the AppDomainSetup.DynamicBase property of the AppDomainSetup object that will be used to create the new application domain. プロパティに割り当てる基本ディレクトリのパスは、単純な名前が AppDomainSetup.ApplicationName プロパティに割り当てる文字列のハッシュコードであるサブディレクトリを追加することによって変更されます。したがって、基本ディレクトリの形式は、ハッシュコード\の元のパスになります。 **.The base directory path you assign to the property is modified by the addition of a subdirectory whose simple name is the hash code of the string you assign to the AppDomainSetup.ApplicationName property, so the format of the base directory is original path\hash code. 動的ディレクトリは、このベースディレクトリのサブディレクトリです。The dynamic directory is a subdirectory of this base directory. 単純な名前は AppDomainSetup.ApplicationName プロパティの値であるため、その形式は\ハッシュコード\アプリケーション名元のパスになります。Its simple name is the value of the AppDomainSetup.ApplicationName property, so its format is original path\hash code\application name.

セキュリティ

FileIOPermission
パス情報にアクセスします。for access to the path information. PathDiscovery (関連する列挙体)Associated enumeration: PathDiscovery.

適用対象