AppDomain.DynamicDirectory Eigenschaft

Definition

Ruft das Verzeichnis ab, das der Assemblyresolver für die Suche nach dynamisch erstellten Assemblys verwendet.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

Eigenschaftswert

Das Verzeichnis, das der Assemblyresolver für die Suche nach dynamisch erstellten Assemblys verwendet.The directory that the assembly resolver uses to probe for dynamically created assemblies.

Implementiert

Ausnahmen

Der Vorgang wird für eine entladene Anwendungsdomäne ausgeführt.The operation is attempted on an unloaded application domain.

Beispiele

Im folgenden Beispiel wird eine Anwendungsdomäne mit einem Verzeichnis für dynamische Assemblys erstellt, eine dynamische Assembly ausgegeben und im dynamischen Verzeichnis gespeichert. Anschließend wird die Assembly in die neue Anwendungsdomäne geladen und verwendet.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.

Im Beispiel wird ein AppDomainSetup Objekt erstellt und dessen ApplicationName-Eigenschaft auf "example" und dessen DynamicBase-Eigenschaft auf "c:\dynamicassemblydir" festgelegt.The example creates an AppDomainSetup object and sets its ApplicationName property to "Example" and its DynamicBase property to "C:\DynamicAssemblyDir". Im Beispiel wird dann die DynamicBase-Eigenschaft angezeigt, um anzuzeigen, dass der Hashcode des Anwendungs namens als Unterverzeichnis des ursprünglich zugewiesenen Pfades angefügt wurde.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.

Hinweis

Das Basisverzeichnis in diesem Beispiel soll außerhalb des Suchpfads für die Beispielanwendung liegen.The base directory in this example is intended to be outside the probing path for the example application. Stellen Sie sicher, dass das Beispiel an einem anderen Speicherort kompiliert wird.Be sure to compile the example in a different location. Löschen Sie jedes Mal, wenn Sie das Beispiel ausführen, das Basisverzeichnis und alle Unterverzeichnisse.Delete the base directory and all its subdirectories each time you run the example.

Im Beispiel wird eine neue Anwendungsdomäne mit dem AppDomainSetup-Objekt erstellt.The example creates a new application domain, using the AppDomainSetup object. Im Beispiel wird die DynamicDirectory-Eigenschaft verwendet, um den Namen des Verzeichnisses abzurufen, damit das Verzeichnis erstellt werden kann.The example uses the DynamicDirectory property to retrieve the name of the directory, so it can create the directory. (In diesem Beispiel kann das Verzeichnis so einfach erstellt werden, indem der ursprüngliche Pfad, der Hashcode des Anwendungs namens und der Anwendungsname verkettet werden.)(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.)

Das Beispiel verfügt über eine GenerateDynamicAssembly-Methode, die eine Assembly mit dem Namen DynamicHelloWorld.dll ausgibt und im dynamischen Verzeichnis der neuen Anwendungsdomäne speichert.The example has a GenerateDynamicAssembly method that emits an assembly named DynamicHelloWorld.dll and stores it in the new application domain's dynamic directory. Die dynamische Assembly enthält einen Typ, HelloWorld, der über eine statische Methode (Shared Methode in Visual Basic) mit dem Namen HelloFromADverfügt.The dynamic assembly contains one type, HelloWorld, that has a static method (Shared method in Visual Basic) named HelloFromAD. Wenn Sie diese Methode aufrufen, wird der Name der Anwendungsdomäne angezeigt.Calling this method displays the name of the application domain.

Die Example-Klasse wird von MarshalByRefObjectabgeleitet, sodass im Beispiel eine Instanz der-Klasse in der neuen Anwendungsdomäne erstellt und die Test-Methode aufgerufen werden kann.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. Die Test-Methode lädt die dynamische Assembly nach Ihrem anzeigen Amen und ruft die statische HelloFromAD Methode auf.The Test method loads the dynamic assembly by its display name and calls the static HelloFromAD method.

Sie können anzeigen, dass das dynamische Verzeichnis nach den normalen Überprüfungs Pfaden durchsucht wird, indem Sie Code für eine Assembly mit dem Namen DynamicHelloWorld.dll schreiben und Sie im gleichen Verzeichnis wie in diesem Beispiel kompilieren.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. Die Assembly muss eine Klasse mit dem Namen HelloWorld mit einer statischen Methode namens HelloFromADhaben.The assembly must have a class named HelloWorld with a static method named HelloFromAD. Diese Methode muss nicht die gleiche Funktionalität wie die im Beispiel haben. Es kann einfach eine Zeichenfolge in der Konsole angezeigt werden.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. Die Assembly muss auch über ein AssemblyVersionAttribute Attribut verfügen, das die Version auf 1.0.0.0 festlegt.The assembly must also have an AssemblyVersionAttribute attribute that sets its version to 1.0.0.0. Wenn Sie das Beispiel ausführen, wird die Assembly, die Sie im aktuellen Verzeichnis kompiliert haben, vor dem Durchsuchen des dynamischen Verzeichnisses gefunden.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'!

Hinweise

Um das dynamische Verzeichnis festzulegen, weisen Sie der AppDomainSetup.DynamicBase-Eigenschaft des AppDomainSetup-Objekts, das zum Erstellen der neuen Anwendungsdomäne verwendet wird, einen Basisverzeichnis Pfad zu.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. Der Basisverzeichnis Pfad, den Sie der-Eigenschaft zuweisen, wird durch das Hinzufügen eines Unterverzeichnisses geändert, dessen einfacher Name der Hashcode der Zeichenfolge ist, die Sie der AppDomainSetup.ApplicationName-Eigenschaft zuweisen, sodass das Format des Basisverzeichnisses der ursprüngliche Pfad\Hashcodeist.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. Das dynamische Verzeichnis ist ein Unterverzeichnis dieses Basisverzeichnisses.The dynamic directory is a subdirectory of this base directory. Der einfache Name ist der Wert der AppDomainSetup.ApplicationName-Eigenschaft, sodass sein Format der ursprüngliche Pfad\Hashcode\Anwendungsnameist.Its simple name is the value of the AppDomainSetup.ApplicationName property, so its format is original path\hash code\application name.

Gilt für: