【问题标题】:Get static List from AppDomain从 AppDomain 获取静态列表
【发布时间】:2015-02-27 09:09:58
【问题描述】:

我想访问 Class2 的静态列表,该列表将在 Class1 中创建。

Class1 加载到一个 AppDomain 中,而 Class2 加载到另一个 AppDomain 中。 但是如果我想访问 Class2 中的静态列表,我会得到两个不同的列表。

我认为我必须访问 Class1 中的同一个 AppDomain 才能获得 Class2,但是如果 Class1 的对象位于不同的 AppDomain 中,我该如何实现呢?

顺便说一句:没有必要将 Class2 放在另一个 AppDomain 中,但我认为如果我愿意,我可以得到相同的对象。

这是我的代码:

public class Class1 : MarshalByRefObject
{
    Class2 class2;

    public Class2 Class2
    {
        get { return class2; }
        set { class2 = value; }
    }
    public Class1()
    {
        AppDomain adc2 = AppDomain.CreateDomain("adc2");
        class2 = (Class2)adc2.CreateInstanceAndUnwrap(typeof(Class2).Assembly.FullName, typeof(Class2).FullName);
    }
}
public class Class2 : MarshalByRefObject
{
    static List<int> myIntegers = new List<int>();

    public static List<int> MyIntegers
    {
        get { return Class2.myIntegers; }
        set { Class2.myIntegers = value; }
    }
    public void AddInteger(int integer)
    {
        myIntegers.Add(integer);
    }
    public override string ToString()
    {
        StringBuilder sb = new StringBuilder();
        foreach (int integer in myIntegers)
        {
            sb.AppendLine(integer.ToString());
        }
        return sb.ToString();
    }
}

class Program
{
    static void Main(string[] args)
    {
        Type type1 = typeof(Class1);
        AppDomain ad1 = AppDomain.CreateDomain("ad1");
        Class1 ad1t1 = (Class1)ad1.CreateInstanceFromAndUnwrap(type1.Assembly.Location, type1.FullName);

        AppDomain ad2 = AppDomain.CreateDomain("ad2");
        Class1 ad2t1 = (Class1)ad2.CreateInstanceFromAndUnwrap(type1.Assembly.Location, type1.FullName);

        ad1t1.Class2.AddInteger(0);
        ad2t1.Class2.AddInteger(1);
        Console.WriteLine(ad1t1.Class2.ToString()); //Output: 0
        Console.WriteLine(ad2t1.Class2.ToString()); //Output: 1
        //
        Console.ReadKey();
    }
}

编辑

好的,我发现我的插件加载器是罪魁祸首。 如果您将使用不同的插件加载器(或至少一个加载器),有人能说出为什么不能跨应用程序域工作吗?

如果所有文件都在一个程序集中,它将起作用(增量为 3)。在我的场景中(许多不同的程序集)我只得到 1,1,1

如果有人需要更多信息来帮助我,请随时提出要求。

示例 1(每个 Instance 自己计算):

程序集:主要

PluginLoader.PluginLoader<IPlugin> pluginLoader1 = new PluginLoader.PluginLoader<IPlugin>(new DirectoryInfo(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)));
IPlugin cl1 = pluginLoader1.Activate("MyLibrary.dll", "MyLibrary.Class1");
PluginLoader.PluginLoader<IPlugin> pluginLoader2 = new PluginLoader.PluginLoader<IPlugin>(new DirectoryInfo(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)));
IPlugin cl3 = pluginLoader2.Activate("MyLibrary2.dll", "MyLibrary2.Class3");
//Increment() increases a static variable starting by 0
cl1.Increment();
Console.WriteLine(cl1.ToString()); //Output: 1
cl3.Increment();
Console.WriteLine(cl3.ToString()); //Output: 1

ClassLibrary2.Class2 class2 = new ClassLibrary2.Class2();
class2.Increment();
Console.WriteLine(class2.ToString()); //Output: 1

程序集:ClassLibrary2

[Serializable]
public class Class2 : IPlugin
{
    public Class2() { }

    public override string ToString()
    {
        return incrementer.ToString();
    }
    static int incrementer = 0;
    public void Increment()
    {
        incrementer++;
    }
}

程序集:我的图书馆

public class Class1 : MarshalByRefObject, IPlugin
{
    Class2 class2;

    public Class2 Class2
    {
        get { return class2; }
        set { class2 = value; }
    }
    public Class1()
    {
        PluginLoader.PluginLoader<Class2> pluginLoader = new PluginLoader.PluginLoader<Class2>(new DirectoryInfo(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)));
        class2 = pluginLoader.Activate("ClassLibrary2.dll", "ClassLibrary2.Class2");
        //AppDomain adc2 = AppDomain.CreateDomain("adc2");
        //class2 = (Class2)adc2.CreateInstanceAndUnwrap(typeof(Class2).Assembly.FullName, typeof(Class2).FullName);
    }
    public void Increment()
    {
        this.class2.Increment();
    }
    public override string ToString()
    {
        return AppDomain.CurrentDomain.FriendlyName+": "+ this.class2.ToString();
    }
}

程序集:MyLibrary2

public class Class3 : MarshalByRefObject, IPlugin
{
    Class2 class2;

    public Class2 Class2
    {
        get { return class2; }
        set { class2 = value; }
    }
    public Class3()
    {
        PluginLoader.PluginLoader<Class2> pluginLoader = new PluginLoader.PluginLoader<Class2>(new DirectoryInfo(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)));
        class2 = pluginLoader.Activate("ClassLibrary2.dll", "ClassLibrary2.Class2");
        //AppDomain adc2 = AppDomain.CreateDomain("adc2");
        //class2 = (Class2)adc2.CreateInstanceAndUnwrap(typeof(Class2).Assembly.FullName, typeof(Class2).FullName);
    }
    public void Increment()
    {
        this.class2.Increment();
    }
    public override string ToString()
    {
        return AppDomain.CurrentDomain.FriendlyName + ": " + this.class2.ToString();
    }
}

程序集:插件接口

public interface IPlugin
{
    void Increment();
}

程序集:插件加载器

public class PluginLoader<T> where T : IPlugin
{
    //Here are placed all Fields
    #region Fields
    string path;
    System.AppDomain appDomain;
    #endregion

    //Here are placed all Properties
    #region Properties
    public List<KeyValuePair<String, String>> Plugins
    {
        get { return (List<KeyValuePair<String, String>>)appDomain.GetData("Plugins"); }
    }
    #endregion

    //Here are placed all Constructors
    #region Constructors
    public PluginLoader(DirectoryInfo path)
    {
        this.path = path.FullName;
        if (!path.Exists)
            path.Create();
        AppDomainSetup appDomainSetup = new AppDomainSetup();
        appDomainSetup.AppDomainInitializer = new AppDomainInitializer(GetInterfaceTypes);
        appDomainSetup.AppDomainInitializerArguments = new string[] { this.path };
        appDomain = AppDomain.CreateDomain(typeof(T).Name, null, appDomainSetup);
    }
    #endregion

    #region Methods
    private static void GetInterfaceTypes(string[] args)
    {
        AppDomain appDomain = System.AppDomain.CurrentDomain;
        string[] files = Directory.GetFiles(args[0], "*.dll", SearchOption.AllDirectories);
        List<KeyValuePair<String, String>> infos = new List<KeyValuePair<String, String>>();
        foreach (string file in files)
        {
            try
            {
                Assembly asm = Assembly.LoadFrom(file);
                foreach (Type type in asm.GetTypes())
                {
                    if (typeof(T).IsAssignableFrom(type))
                        infos.Add(new KeyValuePair<string, string>(file, type.FullName));
                }
            }
            catch (Exception ex)
            {
            }
        }
        appDomain.SetData("Plugins", infos);
    }
    public virtual T Activate(String assemblyFile, String type, params object[] args)
    {
        try
        {
            T instance = (T)this.appDomain.CreateInstanceFromAndUnwrap(Path.Combine(this.path, Path.GetFileName(assemblyFile)), type, args);
            return instance;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    #endregion
}

示例2(每个实例计数相同的增量变量):

将所有类放在一个程序集中。

public interface IPlugin
{
    void Increment();
}
[Serializable]
public class Class1 : IPlugin
{
    public Class1() { }
    static int incrementer = 0;
    public void Increment()
    {
        incrementer++;
    }
    public override string ToString()
    {
        return incrementer.ToString();
    }
}
class PluginLoader<T> where T : IPlugin
{
    //Here are placed all Fields
    #region Fields
    string path;
    System.AppDomain appDomain;
    #endregion

    //Here are placed all Properties
    #region Properties
    public List<KeyValuePair<String, String>> Plugins
    {
        get { return (List<KeyValuePair<String, String>>)appDomain.GetData("Plugins"); }
    }
    #endregion

    //Here are placed all Constructors
    #region Constructors
    public PluginLoader(DirectoryInfo path)
    {
        this.path = path.FullName;
        if (!path.Exists)
            path.Create();
        AppDomainSetup appDomainSetup = new AppDomainSetup();
        appDomainSetup.AppDomainInitializer = new AppDomainInitializer(GetInterfaceTypes);
        appDomainSetup.AppDomainInitializerArguments = new string[] { this.path };
        appDomain = AppDomain.CreateDomain(typeof(T).Name, null, appDomainSetup);
    }
    #endregion

    #region Methods
    private static void GetInterfaceTypes(string[] args)
    {
        AppDomain appDomain = System.AppDomain.CurrentDomain;
        string[] files = Directory.GetFiles(args[0], "*.dll", SearchOption.AllDirectories);
        List<KeyValuePair<String, String>> infos = new List<KeyValuePair<String, String>>();
        foreach (string file in files)
        {
            try
            {
                Assembly asm = Assembly.LoadFrom(file);
                foreach (Type type in asm.GetTypes())
                {
                    if (typeof(T).IsAssignableFrom(type))
                        infos.Add(new KeyValuePair<string, string>(file, type.FullName));
                }
            }
            catch (Exception ex)
            {
            }
        }
        appDomain.SetData("Plugins", infos);
    }
    public virtual T Activate(String assemblyFile, String type, params object[] args)
    {
        try
        {
            T instance = (T)this.appDomain.CreateInstanceFromAndUnwrap(Path.Combine(this.path, Path.GetFileName(assemblyFile)), type, args);
            return instance;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    #endregion
}
class Program
{
    static void Main(string[] args)
    {
        string file = System.Reflection.Assembly.GetExecutingAssembly().Location;
        string path = Path.GetDirectoryName(file);

        PluginLoader<IPlugin> pluginLoader1 = new PluginLoader<IPlugin>(new DirectoryInfo(path));
        IPlugin cl1 = pluginLoader1.Activate(file, "AppDomainCheck.Class1");
        PluginLoader<IPlugin> pluginLoader2 = new PluginLoader<IPlugin>(new DirectoryInfo(path));
        IPlugin cl3 = pluginLoader1.Activate(file, "AppDomainCheck.Class1");

        cl1.Increment();
        Console.WriteLine(cl1.ToString()); //Output: 1
        cl3.Increment();
        Console.WriteLine(cl3.ToString()); //Output: 2

        Console.ReadKey();
    }
}

【问题讨论】:

  • 这可能对你有帮助stackoverflow.com/a/9807826/1505865
  • 我也读过这条评论,但是当我在我的应用程序中尝试它时,它不起作用。所以其他东西与我的代码示例不相等(在上面的示例中有效)。
  • 抱歉在这里粘贴了错误的链接,您的答案似乎发生在他的问题中:) stackoverflow.com/q/4298913/1505865
  • 是的,这就是使用 Serializable 而不是 MarshalByRefObject 的技巧,这也适用于我上面的示例。但是在我的主要应用程序中它不起作用。所以目前我试图找出不同之处。

标签: c# appdomain


【解决方案1】:

静态变量仅限于当前 App 域。如果您有 N 个不同的应用程序域,那么您有 N 个不同的静态属性值。

C# Language Specification 5.0

10.5.1 静态字段不是特定实例的一部分;相反,它在封闭类型的所有实例之间共享(第 4.4.2 节)。无论创建了多少封闭类类型的实例,对于关联的应用程序域,只有一个静态字段的副本

【讨论】:

  • 如果我使用可序列化的类而不是 MarshalByRefObject,我将在每个 appdomain 中获得我的静态变量的副本。但在我的情况下它不起作用。
  • 在您发布的代码中,每次创建一个新的appdomain,所以每次在新创建的appdomain上增加一个值,结果为1。IMO,这种行为是正常的。
  • 如果我将所有文件放在一个程序集中,int 将增加到 3。
  • 如果我将所有类放在不同的程序集中,则整数将计入每个 appDomain。为什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-16
  • 1970-01-01
  • 2013-09-16
  • 1970-01-01
  • 1970-01-01
  • 2014-06-22
  • 1970-01-01
相关资源
最近更新 更多