如何在给定的应用程序域中执行动态创建的程序集?

时间:2016-10-23 23:28:56

标签: c# reflection .net-assembly reflection.emit

尝试在给定的应用程序域中执行动态程序集时遇到问题。程序集正在使用System.Reflection.Emit.AssemblyBuilder类构建,如图所示

    // Utility method for building 'MathClient' assembly in memory. 
    public static void CreateMathClient(AppDomain domainForAssembly)
    {
        // Assembly name... 
        AssemblyName assemblyName = new AssemblyName();
        assemblyName.Name = "MathClient";
        assemblyName.Version = new Version("1.0.0.0");

        AssemblyBuilder assembly = domainForAssembly.DefineDynamicAssembly(
            assemblyName, AssemblyBuilderAccess.RunAndSave);
        ModuleBuilder module = assembly.DefineDynamicModule(
            "MathClient", "MathClient.exe", false);

        // Defining 'Program' class... 
        TypeBuilder programClass = module.DefineType(
            "MathClient.Program",
            TypeAttributes.Public | TypeAttributes.Class); 

        // Defining Main() method... 
        MethodBuilder mainMethod = programClass.DefineMethod(
            "Main",
            MethodAttributes.Public | MethodAttributes.Static,
            null, 
            new Type[] { typeof(string[]) });
        ILGenerator mainMethodILGenerator = mainMethod.GetILGenerator();
        LocalBuilder aLocalVariable = mainMethodILGenerator.DeclareLocal(typeof(int), true);
        mainMethodILGenerator.Emit(OpCodes.Ldc_I4, 10);
        mainMethodILGenerator.Emit(OpCodes.Stloc, aLocalVariable);      // a = 10 

        // List 'a' value... 
        mainMethodILGenerator.Emit(OpCodes.Ldstr, "a = {0}");
        mainMethodILGenerator.Emit(OpCodes.Ldloc, aLocalVariable);
        mainMethodILGenerator.Emit(OpCodes.Box, typeof(int));
        Type consoleType = typeof(System.Console);
        MethodInfo writeLineMethod = consoleType.GetMethod(
            "WriteLine",
            new Type[] { typeof(string), typeof(object) });
        mainMethodILGenerator.Emit(OpCodes.Call, writeLineMethod);

        mainMethodILGenerator.Emit(OpCodes.Ret);

        // Bake 'Program' class type... 
        programClass.CreateType(); 

        // Set Main() method as an entry point...
        assembly.SetEntryPoint(mainMethod, PEFileKinds.ConsoleApplication);

        // Optionally save to disk...
        //assembly.Save("MathClient.exe");
    }

当我尝试在默认应用程序域中执行程序集时

    static void Main(string[] args)
    {
        AppDomain currentDomain = AppDomain.CurrentDomain; 

        // Create new assembly in memory... 
        CreateMathClient(currentDomain);

        // Execute 'MathClient.exe' in the current domain... 
        currentDomain.ExecuteAssemblyByName(
            "MathClient,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null");

        Console.ReadLine();
    }

以下异常抛出

  

System.IO.FileNotFoundException未处理     的HResult = -2147024894     Message =无法加载文件或程序集“MathClient,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null”或其依赖项之一。系统找不到指定的文件。

但是,如果程序集已初步保存到磁盘

        // Optionally save to disk...
        assembly.Save("MathClient.exe");

一切都很顺利。
为什么我不能用ExecuteAssemblyByName()执行程序集?

1 个答案:

答案 0 :(得分:0)

由于您要将其加载到当前AppDomain中,因此您应该能够将其序列化为byte[]并使用Assembly.Load(byte[] bytes)加载它。

MemoryStream stream = new MemoryStream();
formatter.Serialize(stream, assembly);
Assembly.Load(stream.ToArray());