Export (0) Print
Expand All

AppDomainSetup.LoaderOptimization Property

Specifies the optimization policy used to load an executable.

[Visual Basic]
Public Property LoaderOptimization As LoaderOptimization
[C#]
public LoaderOptimization LoaderOptimization {get; set;}
[C++]
public: __property LoaderOptimization get_LoaderOptimization();
public: __property void set_LoaderOptimization(LoaderOptimization);
[JScript]
public function get LoaderOptimization() : LoaderOptimization;
public function set LoaderOptimization(LoaderOptimization);

Property Value

A LoaderOptimization enumerated constant used with the LoaderOptimizationAttribute.

Example

[Visual Basic] 
Imports System
Imports System.IO
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Runtime.Remoting



Class ADDyno
   
   
   
   Public Shared Function CreateADynamicAssembly(ByRef myNewDomain As AppDomain, executableNameNoExe As String) As Type
      
      Dim executableName As String = executableNameNoExe + ".exe"
      
      Dim myAsmName As New AssemblyName()
      myAsmName.Name = executableNameNoExe
      myAsmName.CodeBase = Environment.CurrentDirectory
      
      Dim myAsmBuilder As AssemblyBuilder = myNewDomain.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.RunAndSave)
      Console.WriteLine("-- Dynamic Assembly instantiated.")
      
      Dim myModBuilder As ModuleBuilder = myAsmBuilder.DefineDynamicModule(executableNameNoExe, executableName)
      
      Dim myTypeBuilder As TypeBuilder = myModBuilder.DefineType(executableNameNoExe, TypeAttributes.Public, GetType(MarshalByRefObject))
      
      Dim myFCMethod As MethodBuilder = myTypeBuilder.DefineMethod("CountLocalFiles", MethodAttributes.Public Or MethodAttributes.Static, Nothing, New Type() {})
      
      Dim currentDirGetMI As MethodInfo = GetType(Environment).GetProperty("CurrentDirectory").GetGetMethod()
      Dim writeLine0objMI As MethodInfo = GetType(Console).GetMethod("WriteLine", New Type() {GetType(String)})
      Dim writeLine2objMI As MethodInfo = GetType(Console).GetMethod("WriteLine", New Type() {GetType(String), GetType(Object), GetType(Object)})
      Dim getFilesMI As MethodInfo = GetType(Directory).GetMethod("GetFiles", New Type() {GetType(String)})
      
      myFCMethod.InitLocals = True
      
      Dim myFCIL As ILGenerator = myFCMethod.GetILGenerator()
      
      Console.WriteLine("-- Generating MSIL method body...")
      Dim v0 As LocalBuilder = myFCIL.DeclareLocal(GetType(String))
      Dim v1 As LocalBuilder = myFCIL.DeclareLocal(GetType(Integer))
      Dim v2 As LocalBuilder = myFCIL.DeclareLocal(GetType(String))
      Dim v3 As LocalBuilder = myFCIL.DeclareLocal(GetType(String()))
      
      Dim evalForEachLabel As Label = myFCIL.DefineLabel()
      Dim topOfForEachLabel As Label = myFCIL.DefineLabel()
      
      ' Build the method body.
      myFCIL.EmitCall(OpCodes.Call, currentDirGetMI, Nothing)
      myFCIL.Emit(OpCodes.Stloc_S, v0)
      myFCIL.Emit(OpCodes.Ldc_I4_0)
      myFCIL.Emit(OpCodes.Stloc_S, v1)
      myFCIL.Emit(OpCodes.Ldstr, "---")
      myFCIL.EmitCall(OpCodes.Call, writeLine0objMI, Nothing)
      myFCIL.Emit(OpCodes.Ldloc_S, v0)
      myFCIL.EmitCall(OpCodes.Call, getFilesMI, Nothing)
      myFCIL.Emit(OpCodes.Stloc_S, v3)
      
      myFCIL.Emit(OpCodes.Br_S, evalForEachLabel)
      
      ' foreach loop starts here.
      myFCIL.MarkLabel(topOfForEachLabel)
      
      ' Load array of strings and index, store value at index for output.
      myFCIL.Emit(OpCodes.Ldloc_S, v3)
      myFCIL.Emit(OpCodes.Ldloc_S, v1)
      myFCIL.Emit(OpCodes.Ldelem_Ref)
      myFCIL.Emit(OpCodes.Stloc_S, v2)
      
      myFCIL.Emit(OpCodes.Ldloc_S, v2)
      myFCIL.EmitCall(OpCodes.Call, writeLine0objMI, Nothing)
      
      ' Increment counter by one.
      myFCIL.Emit(OpCodes.Ldloc_S, v1)
      myFCIL.Emit(OpCodes.Ldc_I4_1)
      myFCIL.Emit(OpCodes.Add)
      myFCIL.Emit(OpCodes.Stloc_S, v1)
      
      ' Determine if end of file list array has been reached.
      myFCIL.MarkLabel(evalForEachLabel)
      myFCIL.Emit(OpCodes.Ldloc_S, v1)
      myFCIL.Emit(OpCodes.Ldloc_S, v3)
      myFCIL.Emit(OpCodes.Ldlen)
      myFCIL.Emit(OpCodes.Conv_I4)
      myFCIL.Emit(OpCodes.Blt_S, topOfForEachLabel)
      'foreach loop end here.
      myFCIL.Emit(OpCodes.Ldstr, "---")
      myFCIL.EmitCall(OpCodes.Call, writeLine0objMI, Nothing)
      myFCIL.Emit(OpCodes.Ldstr, "There are {0} files in {1}.")
      myFCIL.Emit(OpCodes.Ldloc_S, v1)
      myFCIL.Emit(OpCodes.Box, GetType(Integer))
      myFCIL.Emit(OpCodes.Ldloc_S, v0)
      myFCIL.EmitCall(OpCodes.Call, writeLine2objMI, Nothing)
      
      myFCIL.Emit(OpCodes.Ret)
      
      Dim myType As Type = myTypeBuilder.CreateType()
      
      myAsmBuilder.SetEntryPoint(myFCMethod)
      myAsmBuilder.Save(executableName)
      Console.WriteLine("-- Method generated, type completed, and assembly saved to disk.")
      
      Return myType
   End Function 'CreateADynamicAssembly
    
   
   Public Shared Sub Main()
      
      Dim executableName As String = Nothing
      Dim domainDir As String
      
      Console.Write("Enter a name for the file counting assembly: ")
      Dim executableNameNoExe As String = Console.ReadLine()
      executableName = executableNameNoExe + ".exe"
      Console.WriteLine("---")
      
      domainDir = Environment.CurrentDirectory
      
      Dim curDomain As AppDomain = Thread.GetDomain()
      
      
      ' Create a new AppDomain, with the current directory as the base.
      Console.WriteLine("Current Directory: {0}", Environment.CurrentDirectory)
      Dim mySetupInfo As New AppDomainSetup()
      mySetupInfo.ApplicationBase = domainDir
      mySetupInfo.ApplicationName = executableNameNoExe
      mySetupInfo.LoaderOptimization = LoaderOptimization.SingleDomain
      
      Dim myDomain As AppDomain = AppDomain.CreateDomain(executableNameNoExe, Nothing, mySetupInfo)
      
      Console.WriteLine("Creating a new AppDomain '{0}'...", executableNameNoExe)
      
      Console.WriteLine("-- Base Directory = '{0}'", myDomain.BaseDirectory)
      Console.WriteLine("-- Shadow Copy? = '{0}'", myDomain.ShadowCopyFiles)
      
      Console.WriteLine("---")
      Dim myFCType As Type = CreateADynamicAssembly(curDomain, executableNameNoExe)
      
      Console.WriteLine("Loading '{0}' from '{1}'...", executableName, myDomain.BaseDirectory.ToString())
      
      
      Dim bFlags As BindingFlags = BindingFlags.Public Or BindingFlags.CreateInstance Or BindingFlags.Instance
      
      Dim myObjInstance As [Object] = myDomain.CreateInstanceAndUnwrap(executableNameNoExe, executableNameNoExe, False, bFlags, Nothing, Nothing, Nothing, Nothing, Nothing)
      
      Console.WriteLine("Executing method 'CountLocalFiles' in {0}...", myObjInstance.ToString())
      
      myFCType.InvokeMember("CountLocalFiles", BindingFlags.InvokeMethod, Nothing, myObjInstance, New Object() {})
   End Sub 'Main
End Class 'ADDyno 




[C#] 

using System;
using System.IO;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.Remoting;

class ADDyno

{

   public static Type CreateADynamicAssembly(ref AppDomain myNewDomain,
                         string executableNameNoExe)
   {

    string executableName = executableNameNoExe + ".exe";

    AssemblyName myAsmName = new AssemblyName();
    myAsmName.Name = executableNameNoExe;
    myAsmName.CodeBase = Environment.CurrentDirectory;

    AssemblyBuilder myAsmBuilder = myNewDomain.DefineDynamicAssembly(myAsmName,
                        AssemblyBuilderAccess.RunAndSave);
    Console.WriteLine("-- Dynamic Assembly instantiated.");

    ModuleBuilder myModBuilder = myAsmBuilder.DefineDynamicModule(executableNameNoExe,
                                      executableName);

    TypeBuilder myTypeBuilder = myModBuilder.DefineType(executableNameNoExe,
                        TypeAttributes.Public,
                        typeof(MarshalByRefObject));

    MethodBuilder myFCMethod = myTypeBuilder.DefineMethod("CountLocalFiles",
                        MethodAttributes.Public |
                        MethodAttributes.Static,
                        null,
                        new Type[] {  });

    MethodInfo currentDirGetMI = typeof(Environment).GetProperty("CurrentDirectory").GetGetMethod();
    MethodInfo writeLine0objMI = typeof(Console).GetMethod("WriteLine",
                     new Type[] { typeof(string) });
    MethodInfo writeLine2objMI = typeof(Console).GetMethod("WriteLine",
                     new Type[] { typeof(string), typeof(object), typeof(object) });
    MethodInfo getFilesMI = typeof(Directory).GetMethod("GetFiles", 
                new Type[] { typeof(string) });

    myFCMethod.InitLocals = true;

    ILGenerator myFCIL = myFCMethod.GetILGenerator();

    Console.WriteLine("-- Generating MSIL method body...");
    LocalBuilder v0 = myFCIL.DeclareLocal(typeof(string));
    LocalBuilder v1 = myFCIL.DeclareLocal(typeof(int));
    LocalBuilder v2 = myFCIL.DeclareLocal(typeof(string));
    LocalBuilder v3 = myFCIL.DeclareLocal(typeof(string[]));

    Label evalForEachLabel = myFCIL.DefineLabel();
    Label topOfForEachLabel = myFCIL.DefineLabel();

    // Build the method body.

    myFCIL.EmitCall(OpCodes.Call, currentDirGetMI, null);
    myFCIL.Emit(OpCodes.Stloc_S, v0);
    myFCIL.Emit(OpCodes.Ldc_I4_0);
    myFCIL.Emit(OpCodes.Stloc_S, v1);
    myFCIL.Emit(OpCodes.Ldstr, "---");
    myFCIL.EmitCall(OpCodes.Call, writeLine0objMI, null);
    myFCIL.Emit(OpCodes.Ldloc_S, v0);
    myFCIL.EmitCall(OpCodes.Call, getFilesMI, null);
    myFCIL.Emit(OpCodes.Stloc_S, v3);

    myFCIL.Emit(OpCodes.Br_S, evalForEachLabel);

    // foreach loop starts here.
    myFCIL.MarkLabel(topOfForEachLabel);
    
        // Load array of strings and index, store value at index for output.
    myFCIL.Emit(OpCodes.Ldloc_S, v3);
    myFCIL.Emit(OpCodes.Ldloc_S, v1);
    myFCIL.Emit(OpCodes.Ldelem_Ref);
    myFCIL.Emit(OpCodes.Stloc_S, v2);

    myFCIL.Emit(OpCodes.Ldloc_S, v2);
    myFCIL.EmitCall(OpCodes.Call, writeLine0objMI, null);

    // Increment counter by one.
    myFCIL.Emit(OpCodes.Ldloc_S, v1);
    myFCIL.Emit(OpCodes.Ldc_I4_1);
    myFCIL.Emit(OpCodes.Add);
    myFCIL.Emit(OpCodes.Stloc_S, v1);

    // Determine if end of file list array has been reached.
    myFCIL.MarkLabel(evalForEachLabel);
    myFCIL.Emit(OpCodes.Ldloc_S, v1);
    myFCIL.Emit(OpCodes.Ldloc_S, v3);
    myFCIL.Emit(OpCodes.Ldlen);
    myFCIL.Emit(OpCodes.Conv_I4);
    myFCIL.Emit(OpCodes.Blt_S, topOfForEachLabel);
    //foreach loop end here.

    myFCIL.Emit(OpCodes.Ldstr, "---");
    myFCIL.EmitCall(OpCodes.Call, writeLine0objMI, null);
    myFCIL.Emit(OpCodes.Ldstr, "There are {0} files in {1}.");
    myFCIL.Emit(OpCodes.Ldloc_S, v1);
    myFCIL.Emit(OpCodes.Box, typeof(int));
    myFCIL.Emit(OpCodes.Ldloc_S, v0);
    myFCIL.EmitCall(OpCodes.Call, writeLine2objMI, null);

    myFCIL.Emit(OpCodes.Ret);

    Type myType = myTypeBuilder.CreateType();

    myAsmBuilder.SetEntryPoint(myFCMethod);
    myAsmBuilder.Save(executableName);        
    Console.WriteLine("-- Method generated, type completed, and assembly saved to disk."); 

    return myType;

   }

   public static void Main() 
   {

    string domainDir, executableName = null;
    
    Console.Write("Enter a name for the file counting assembly: ");
    string executableNameNoExe = Console.ReadLine();
    executableName = executableNameNoExe + ".exe";
    Console.WriteLine("---");

    domainDir = Environment.CurrentDirectory;

    AppDomain curDomain = Thread.GetDomain();    


    // Create a new AppDomain, with the current directory as the base.

    Console.WriteLine("Current Directory: {0}", Environment.CurrentDirectory);
    AppDomainSetup mySetupInfo = new AppDomainSetup();
    mySetupInfo.ApplicationBase = domainDir;
    mySetupInfo.ApplicationName = executableNameNoExe;
    mySetupInfo.LoaderOptimization = LoaderOptimization.SingleDomain;

    AppDomain myDomain = AppDomain.CreateDomain(executableNameNoExe,
                    null, mySetupInfo);

    Console.WriteLine("Creating a new AppDomain '{0}'...",
                    executableNameNoExe);

    Console.WriteLine("-- Base Directory = '{0}'", myDomain.BaseDirectory); 
    Console.WriteLine("-- Shadow Copy? = '{0}'", myDomain.ShadowCopyFiles); 

    Console.WriteLine("---");
    Type myFCType = CreateADynamicAssembly(ref curDomain, 
                     executableNameNoExe);

    Console.WriteLine("Loading '{0}' from '{1}'...", executableName,
              myDomain.BaseDirectory.ToString());


    BindingFlags bFlags = (BindingFlags.Public | BindingFlags.CreateInstance |
                   BindingFlags.Instance);

    Object myObjInstance = myDomain.CreateInstanceAndUnwrap(executableNameNoExe,
                executableNameNoExe, false, bFlags, 
                null, null, null, null, null);

    Console.WriteLine("Executing method 'CountLocalFiles' in {0}...",
               myObjInstance.ToString());

    myFCType.InvokeMember("CountLocalFiles", BindingFlags.InvokeMethod, null,
                myObjInstance, new object[] { });
            
        
   }

}


[C++] 

#using <mscorlib.dll>
using namespace System;
using namespace System::IO;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Runtime::Remoting;

__gc class ADDyno

{

public:
   static Type* CreateADynamicAssembly(AppDomain** myNewDomain,
      String* executableNameNoExe)
   {

      String* executableName = String::Concat( executableNameNoExe, S".exe" );

      AssemblyName* myAsmName = new AssemblyName();
      myAsmName->Name = executableNameNoExe;
      myAsmName->CodeBase = Environment::CurrentDirectory;

      AssemblyBuilder* myAsmBuilder = (*myNewDomain)->DefineDynamicAssembly(myAsmName,
         AssemblyBuilderAccess::RunAndSave);
      Console::WriteLine(S"-- Dynamic Assembly instantiated.");

      ModuleBuilder* myModBuilder = myAsmBuilder->DefineDynamicModule(executableNameNoExe,
         executableName);

      TypeBuilder* myTypeBuilder = myModBuilder->DefineType(executableNameNoExe,
         TypeAttributes::Public,
         __typeof(MarshalByRefObject));

      Type* temp0 [] = 0;

      MethodBuilder* myFCMethod = myTypeBuilder->DefineMethod(S"CountLocalFiles",
         static_cast<MethodAttributes>(MethodAttributes::Public | MethodAttributes::Static),
         0,
         temp0);

      MethodInfo* currentDirGetMI = __typeof(Environment)->GetProperty(S"CurrentDirectory")->GetGetMethod();

      Type* temp1 [] = {__typeof(String)};

      MethodInfo* writeLine0objMI = __typeof(Console)->GetMethod(S"WriteLine",
         temp1);

      Type* temp2 [] = {__typeof(String), __typeof(Object), __typeof(Object)};

      MethodInfo* writeLine2objMI = __typeof(Console)->GetMethod(S"WriteLine",
         temp2);

      Type* temp3 [] = {__typeof(String)};

      MethodInfo* getFilesMI = __typeof(Directory)->GetMethod(S"GetFiles", 
         temp3);

      myFCMethod->InitLocals = true;

      ILGenerator* myFCIL = myFCMethod->GetILGenerator();

      Console::WriteLine(S"-- Generating MSIL method body...");
      LocalBuilder* v0 = myFCIL->DeclareLocal(__typeof(String));
      LocalBuilder* v1 = myFCIL->DeclareLocal(__typeof(int));
      LocalBuilder* v2 = myFCIL->DeclareLocal(__typeof(String));
      LocalBuilder* v3 = myFCIL->DeclareLocal(__typeof(String*[]));

      Label evalForEachLabel = myFCIL->DefineLabel();
      Label topOfForEachLabel = myFCIL->DefineLabel();

      // Build the method body.

      myFCIL->EmitCall(OpCodes::Call, currentDirGetMI, 0);
      myFCIL->Emit(OpCodes::Stloc_S, v0);
      myFCIL->Emit(OpCodes::Ldc_I4_0);
      myFCIL->Emit(OpCodes::Stloc_S, v1);
      myFCIL->Emit(OpCodes::Ldstr, S"---");
      myFCIL->EmitCall(OpCodes::Call, writeLine0objMI, 0);
      myFCIL->Emit(OpCodes::Ldloc_S, v0);
      myFCIL->EmitCall(OpCodes::Call, getFilesMI, 0);
      myFCIL->Emit(OpCodes::Stloc_S, v3);

      myFCIL->Emit(OpCodes::Br_S, evalForEachLabel);

      // foreach loop starts here.
      myFCIL->MarkLabel(topOfForEachLabel);

      // Load array of strings and index, store value at index for output.
      myFCIL->Emit(OpCodes::Ldloc_S, v3);
      myFCIL->Emit(OpCodes::Ldloc_S, v1);
      myFCIL->Emit(OpCodes::Ldelem_Ref);
      myFCIL->Emit(OpCodes::Stloc_S, v2);

      myFCIL->Emit(OpCodes::Ldloc_S, v2);
      myFCIL->EmitCall(OpCodes::Call, writeLine0objMI, 0);

      // Increment counter by one.
      myFCIL->Emit(OpCodes::Ldloc_S, v1);
      myFCIL->Emit(OpCodes::Ldc_I4_1);
      myFCIL->Emit(OpCodes::Add);
      myFCIL->Emit(OpCodes::Stloc_S, v1);

      // Determine if end of file list array has been reached.
      myFCIL->MarkLabel(evalForEachLabel);
      myFCIL->Emit(OpCodes::Ldloc_S, v1);
      myFCIL->Emit(OpCodes::Ldloc_S, v3);
      myFCIL->Emit(OpCodes::Ldlen);
      myFCIL->Emit(OpCodes::Conv_I4);
      myFCIL->Emit(OpCodes::Blt_S, topOfForEachLabel);
      //foreach loop end here.

      myFCIL->Emit(OpCodes::Ldstr, S"---");
      myFCIL->EmitCall(OpCodes::Call, writeLine0objMI, 0);
      myFCIL->Emit(OpCodes::Ldstr, S"There are {0} files in {1}.");
      myFCIL->Emit(OpCodes::Ldloc_S, v1);
      myFCIL->Emit(OpCodes::Box, __typeof(int));
      myFCIL->Emit(OpCodes::Ldloc_S, v0);
      myFCIL->EmitCall(OpCodes::Call, writeLine2objMI, 0);

      myFCIL->Emit(OpCodes::Ret);

      Type* myType = myTypeBuilder->CreateType();

      myAsmBuilder->SetEntryPoint(myFCMethod);
      myAsmBuilder->Save(executableName);        
      Console::WriteLine(S"-- Method generated, type completed, and assembly saved to disk."); 

      return myType;
   }
};

int main() 
{

   String* domainDir;
   String* executableName = 0;

   Console::Write(S"Enter a name for the file counting assembly: ");
   String* executableNameNoExe = Console::ReadLine();
   executableName = String::Concat( executableNameNoExe, S".exe" );
   Console::WriteLine(S"---");

   domainDir = Environment::CurrentDirectory;

   AppDomain* curDomain = Thread::GetDomain();    


   // Create a new AppDomain, with the current directory as the base.

   Console::WriteLine(S"Current Directory: {0}",Environment::CurrentDirectory);
   AppDomainSetup* mySetupInfo = new AppDomainSetup();
   mySetupInfo->ApplicationBase = domainDir;
   mySetupInfo->ApplicationName = executableNameNoExe;
   mySetupInfo->LoaderOptimization = LoaderOptimization::SingleDomain;

   AppDomain* myDomain = AppDomain::CreateDomain(executableNameNoExe,
      0, mySetupInfo);

   Console::WriteLine(S"Creating a new AppDomain '{0}'...",executableNameNoExe);

   Console::WriteLine(S"-- Base Directory = '{0}'",myDomain->BaseDirectory); 
   Console::WriteLine(S"-- Shadow Copy? = '{0}'", __box(myDomain->ShadowCopyFiles)); 

   Console::WriteLine(S"---");
   Type* myFCType = ADDyno::CreateADynamicAssembly(&curDomain, 
      executableNameNoExe);

   Console::WriteLine(S"Loading '{0}' from '{1}'...",executableName,myDomain->BaseDirectory);


   BindingFlags bFlags = static_cast<BindingFlags>
      (BindingFlags::Public | BindingFlags::CreateInstance | BindingFlags::Instance);

   Object* myObjInstance = myDomain->CreateInstanceAndUnwrap(executableNameNoExe,
      executableNameNoExe, false, bFlags, 
      0, 0, 0, 0, 0);

   Console::WriteLine(S"Executing method 'CountLocalFiles' in {0}...",myObjInstance);


   Object* temp4 [] = 0;

   myFCType->InvokeMember(S"CountLocalFiles", BindingFlags::InvokeMethod, 0,
      myObjInstance, temp4);

}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

AppDomainSetup Class | AppDomainSetup Members | System Namespace

Show:
© 2014 Microsoft