14

我编写了一小段关于动态加载程序集和从这些程序集创建类实例的代码,包括一个可执行文件、一个要动态加载的测试库和一个加载器库,用于将动态程序集加载到新的Appdomain. 加载程序库被可执行文件和动态库引用。

//executable
[System.STAThreadAttribute()]
[System.LoaderOptimization(LoaderOptimization.MultiDomain)]
static void Main(string[] args)
{       
    AppDomainSetup domainSetup = new AppDomainSetup()
    {
        ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
        ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile,
        ApplicationName = AppDomain.CurrentDomain.SetupInformation.ApplicationName,
        LoaderOptimization = LoaderOptimization.MultiDomain
    };
    AppDomain childDomain = AppDomain.CreateDomain("MyDomain", null, domainSetup);
    Console.WriteLine(AppDomain.CurrentDomain.SetupInformation.LoaderOptimization.ToString());
    Console.WriteLine(childDomain.SetupInformation.LoaderOptimization.ToString());

    byte[] assembly = null;
    string assemblyName = "CSTestLib"; 

    using (FileStream fs = new FileStream(assemblyName+".dll",FileMode.Open))
    {
        byte[] byt = new byte[fs.Length];
        fs.Read(byt,0,(int)fs.Length);
        assembly = byt;          
    }

    object[] pararmeters = {assemblyName,assembly}; 
    string LoaderAssemblyName = typeof(AssemblyLoader).Assembly.FullName;
    string LoaderClassName = typeof(AssemblyLoader).FullName;
    AssemblyLoader assloader = (AssemblyLoader)childDomain.CreateInstanceAndUnwrap(LoaderAssemblyName,LoaderClassName , true, BindingFlags.CreateInstance, null, parameters, null, null);


    object obj = assloader.Load("CSTestLib.Class1");
    object obj2 = assloader.Load("CSTestLib.Class2");

    AppDomain.Unload(childDomain);

    Console.ReadKey();
}

//Dynamic Lib
using System;


namespace CSTestLib
{
    public class Class1 :MarshalByRefObject
    {
        public Class1() { }
    }



    public class Class2 : MarshalByRefObject
    {
        public Class2() { }
    }
}

//Loader Library


using System;

namespace LoaderLibrary
{
    public class AssemblyLoader : MarshalByRefObject
    {
        string assemblyName;
        public AssemblyLoader(string assName, byte[] ass)
        {
            assemblyName = assName;
            AppDomain.CurrentDomain.Load(ass);
            Console.WriteLine(AppDomain.CurrentDomain.FriendlyName + " " + AppDomain.CurrentDomain.SetupInformation.LoaderOptimization.ToString());
        }

        public object Load(string className)
        {
            object ret = null;
            try
            {
                ret = AppDomain.CurrentDomain.CreateInstanceAndUnwrap(assemblyName, className);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return ret;
        }
    }
}
  1. 在这里,我设置LoaderOptimizationAttributemain()方法,但AppDomain.CurrentDomain.SetupInformation.LoaderOptimization.ToString();说它是NotSpecified为什么?

  2. MultiDomain和之间的区别MultiDomainHost对我来说不是很清楚。MultiDomainHost仅适用于 GAC 程序集吗?适合我的情况哪个更合适?

  3. 据此_

    JIT 编译的代码不能用于加载到加载源上下文、使用 Assembly 类的 LoadFrom 方法或使用指定字节数组的 Load 方法的重载从图像加载的程序集。

那么如何检测程序集是否加载了域中性?如何确保我加载的是域中立的?

4

1 回答 1

15

仅当您使用NGen预编译程序集以加快应用程序的热启动时,此属性才有效。当您指定MultiDomainMultiDomainHost启用预编译(ngenned)程序集的使用时。您可以使用Process Explorer验证这一点,您可以在其中查看已加载模块的列表。

如果您的应用程序由多个共享程序集的可执行实例组成,这是最大的启动时间节省之一。这使 .NET 能够在进程之间共享代码页,从而节省实际内存(一个程序集在物理内存中仅存在一次,但它在一个或多个进程之间共享)并防止在每个进程中一遍又一遍地 JIT 相同的代码这需要时间,代价是生成的代码效率稍低一些,因为它可能是使用常规 JIT 编译的,后者可以使用更多动态数据来生成最有效的代码。

在您的示例中,您将程序集加载到位于托管堆中的字节数组中并增加您的私有字节数。这使得进程之间无法共享数据。只有在您的硬盘上有对应的只读页面才能在进程之间共享。这就是属性没有效果的原因。如果您追求的是热启动性能的 2 倍,那么这就是您要寻找的属性。对于其他任何事情都无关紧要。

现在回到你原来的问题:

  1. 它已设置,但是当您在调试器下启动应用程序时,此MultiDomain属性将被忽略。当您在调试器之外启动它时,您将获得预期的结果。
  2. 是的,仅对已签名的程序集MultiDomainHost启用中立性,所有其他程序集均不共享。AppDomain
  3. Code sharing can only happen when it is precompiled. The real question is: How to check if the assembly is precompiled? I do it with Process Explorer by looking at the list of loaded modules. When my loaded assembly shows up with a path to the Native Image cache and an .ni extension I am sure the precompiled image is beeing used. You can check this also with fuslogvw when you set the radio button to Native Images to check why a native images was not used by the runtime.
于 2011-04-25T21:17:55.210 回答