静态/全局变量的内存隔离(多个实例化)

时间:2018-12-10 09:08:22

标签: c# static factory isolation appdomainsetup

我使用带有一些静态成员的第三方类:

class TheirClass{ //from metadata
    static TheirType TheirProp
}

我在代码中使用该类的实例

class MyWorkLoad  {
    TheirClass theInstance
}

但是我需要单独的上下文/域/任何内容

{//My Program
    var w1 = MyWorkFactory.Create();
        w1.doWorkBasedOnFreshContext()

    var w2 = MyWorkFactory.Create();    
        w2.doWorkBasedOnFreshContext(); //err, dirty context, shared with w1
 }

我尝试创建子AppDomain,但是仍然出现相同的行为:

protected static AppDomain CreatedChildDomain(AppDomain parentDomain)
{
  Evidence evidence = new Evidence(parentDomain.Evidence);
  AppDomainSetup setup = parentDomain.SetupInformation;
  AppDomain.CreateDomain("Lab_"+generationCount, evidence, setup);
}

这是我用来实例化的工厂方法

public static MyWorkLoad Create(string LabConfigPath)
{
    AppDomain subdomain = CreatedChildDomain(AppDomain.CurrentDomain);

    ExeConfigurationFileMap fileMap =
        new ExeConfigurationFileMap();

    fileMap.ExeConfigFilename = LabConfigPath;
    var config =
        ConfigurationManager.OpenMappedExeConfiguration(fileMap,
            ConfigurationUserLevel.None);

    var conf = config.AppSettings.Settings;
    var nextWork = new MyWorkLoad(subdomain, conf);
    generationCount++;
    return nextWork;
}

如何实现所需的隔离行为?

0 个答案:

没有答案