AppDomainSetup.LoaderOptimization Propriété

Définition

Spécifie la stratégie d'optimisation utilisée pour charger un exécutable.

public:
 property LoaderOptimization LoaderOptimization { LoaderOptimization get(); void set(LoaderOptimization value); };
public LoaderOptimization LoaderOptimization { get; set; }
member this.LoaderOptimization : LoaderOptimization with get, set
Public Property LoaderOptimization As LoaderOptimization

Valeur de propriété

Constante énumérée utilisée avec LoaderOptimizationAttribute.

Exemples

L’exemple suivant crée un assembly dynamique et l’enregistre sur le disque, puis utilise la LoaderOptimization propriété pour définir la stratégie d’optimisation utilisée pour charger l’assembly dans un domaine d’application.

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;

ref class ADDyno
{
public:
   static Type^ CreateADynamicAssembly( interior_ptr<AppDomain^> myNewDomain, String^ executableNameNoExe )
   {
      String^ executableName = String::Concat( executableNameNoExe, ".exe" );
      AssemblyName^ myAsmName = gcnew 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, MarshalByRefObject::typeid );
      array<Type^>^temp0 = nullptr;
      MethodBuilder^ myFCMethod = myTypeBuilder->DefineMethod( "CountLocalFiles", static_cast<MethodAttributes>(MethodAttributes::Public | MethodAttributes::Static), nullptr, temp0 );
      MethodInfo^ currentDirGetMI = Environment::typeid->GetProperty( "CurrentDirectory" )->GetGetMethod();
      array<Type^>^temp1 = {String::typeid};
      MethodInfo^ writeLine0objMI = Console::typeid->GetMethod( "WriteLine", temp1 );
      array<Type^>^temp2 = {String::typeid,Object::typeid,Object::typeid};
      MethodInfo^ writeLine2objMI = Console::typeid->GetMethod( "WriteLine", temp2 );
      array<Type^>^temp3 = {String::typeid};
      MethodInfo^ getFilesMI = Directory::typeid->GetMethod( "GetFiles", temp3 );
      myFCMethod->InitLocals = true;
      ILGenerator^ myFCIL = myFCMethod->GetILGenerator();
      Console::WriteLine( "-- Generating MSIL method body..." );
      LocalBuilder^ v0 = myFCIL->DeclareLocal( String::typeid );
      LocalBuilder^ v1 = myFCIL->DeclareLocal( int::typeid );
      LocalBuilder^ v2 = myFCIL->DeclareLocal( String::typeid );
      LocalBuilder^ v3 = myFCIL->DeclareLocal( array<String^>::typeid );
      Label evalForEachLabel = myFCIL->DefineLabel();
      Label topOfForEachLabel = myFCIL->DefineLabel();

      // Build the method body.
      myFCIL->EmitCall( OpCodes::Call, currentDirGetMI, nullptr );
      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, nullptr );
      myFCIL->Emit( OpCodes::Ldloc_S, v0 );
      myFCIL->EmitCall( OpCodes::Call, getFilesMI, nullptr );
      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, nullptr );

      // 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, nullptr );
      myFCIL->Emit( OpCodes::Ldstr, "There are {0} files in {1}." );
      myFCIL->Emit( OpCodes::Ldloc_S, v1 );
      myFCIL->Emit( OpCodes::Box, int::typeid );
      myFCIL->Emit( OpCodes::Ldloc_S, v0 );
      myFCIL->EmitCall( OpCodes::Call, writeLine2objMI, nullptr );
      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;
   }
};

int main()
{
   String^ domainDir;
   String^ executableName = nullptr;
   Console::Write( "Enter a name for the file counting assembly: " );
   String^ executableNameNoExe = Console::ReadLine();
   executableName = String::Concat( 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 = gcnew AppDomainSetup;
   mySetupInfo->ApplicationBase = domainDir;
   mySetupInfo->ApplicationName = executableNameNoExe;
   mySetupInfo->LoaderOptimization = LoaderOptimization::SingleDomain;
   AppDomain^ myDomain = AppDomain::CreateDomain( executableNameNoExe, nullptr, 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 = ADDyno::CreateADynamicAssembly(  &curDomain, executableNameNoExe );
   Console::WriteLine( "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, nullptr, nullptr, nullptr, nullptr, nullptr );
   Console::WriteLine( "Executing method 'CountLocalFiles' in {0}...", myObjInstance );
   array<Object^>^temp4 = nullptr;
   myFCType->InvokeMember( "CountLocalFiles", BindingFlags::InvokeMethod, nullptr, myObjInstance, temp4 );
}
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[] { });
   }
}
open System
open System.IO
open System.Threading
open System.Reflection
open System.Reflection.Emit

let createADynamicAssembly (myNewDomain: byref<AppDomain>) executableNameNoExe =
    let executableName = executableNameNoExe + ".exe"

    let myAsmName = AssemblyName()
    myAsmName.Name <- executableNameNoExe
    myAsmName.CodeBase <- Environment.CurrentDirectory

    let myAsmBuilder = 
        myNewDomain.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.RunAndSave)
    printfn "-- Dynamic Assembly instantiated."

    let myModBuilder = 
        myAsmBuilder.DefineDynamicModule(executableNameNoExe, executableName)

    let myTypeBuilder = 
        myModBuilder.DefineType(executableNameNoExe,
                        TypeAttributes.Public,
                        typeof<MarshalByRefObject>)

    let myFCMethod = 
        myTypeBuilder.DefineMethod("CountLocalFiles",
                        MethodAttributes.Public |||
                        MethodAttributes.Static,
                        null,
                        [||])

    let currentDirGetMI = typeof<Environment>.GetProperty("CurrentDirectory").GetGetMethod()
    let writeLine0objMI = typeof<Console>.GetMethod("WriteLine", [| typeof<string> |])
    let writeLine2objMI = typeof<Console>.GetMethod("WriteLine", [| typeof<string>; typeof<obj>; typeof<obj> |])
    let getFilesMI = typeof<Directory>.GetMethod("GetFiles", [| typeof<string> |])

    myFCMethod.InitLocals <- true

    let myFCIL = myFCMethod.GetILGenerator()

    printfn "-- Generating MSIL method body..."
    let v0 = myFCIL.DeclareLocal typeof<string>
    let v1 = myFCIL.DeclareLocal typeof<int>
    let v2 = myFCIL.DeclareLocal typeof<string>
    let v3 = myFCIL.DeclareLocal typeof<string[]>

    let evalForEachLabel = myFCIL.DefineLabel()
    let 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

    let myType = myTypeBuilder.CreateType()

    myAsmBuilder.SetEntryPoint myFCMethod
    myAsmBuilder.Save executableName
    printfn "-- Method generated, type completed, and assembly saved to disk."

    myType


printf "Enter a name for the file counting assembly: "
let executableNameNoExe = stdin.ReadLine()
let executableName = executableNameNoExe + ".exe"
printfn "---"

let domainDir = Environment.CurrentDirectory

let mutable curDomain = Thread.GetDomain()

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

printfn $"Current Directory: {Environment.CurrentDirectory}"
let mySetupInfo = AppDomainSetup()
mySetupInfo.ApplicationBase <- domainDir
mySetupInfo.ApplicationName <- executableNameNoExe
mySetupInfo.LoaderOptimization <- LoaderOptimization.SingleDomain

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

printfn $"Creating a new AppDomain '{executableNameNoExe}'..."

printfn $"-- Base Directory = '{myDomain.BaseDirectory}'"
printfn $"-- Shadow Copy? = '{myDomain.ShadowCopyFiles}'"

printfn "---"
let myFCType = 
    createADynamicAssembly &curDomain executableNameNoExe

printfn $"Loading '{executableName}' from '{myDomain.BaseDirectory}'..."

let bFlags = 
    BindingFlags.Public ||| BindingFlags.CreateInstance ||| BindingFlags.Instance

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

printfn $"Executing method 'CountLocalFiles' in {myObjInstance}..."

myFCType.InvokeMember("CountLocalFiles", BindingFlags.InvokeMethod, null, myObjInstance, [||])
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
End Class

S’applique à