Static Fields in AppDomain

前端 未结 1 1994
攒了一身酷
攒了一身酷 2020-11-28 08:05

I\'m experimenting ideas around using AppDomain to manage some legacy code contains lots of static fields in a multi-threaded environment.

I read answers this questi

相关标签:
1条回答
  • 2020-11-28 08:48

    It looks like you are loading a type from another appDomain into the current appDomain. Thus the code that calls the static methods are calling from the current appDomain.

    I'm unaware of any other way to call a static method in another domain without creating an instance of an object in another domain, and having that object call the static method.

    Example: Solution contains 2 Projects (ClassLibrary and a Winforms/Console app)

    [ClassLibrary]

    using System;
    
    namespace MyLibrary
    {
        public class DomainObject : MarshalByRefObject
        {
            private static int _Value;
    
            private static void IncrementValue()
            {
                DomainObject._Value++;
            }
    
            public static int Value
            {
                get
                {
                    return DomainObject._Value;
                }
            }
    
            public int GetIncrementedValue()
            {
                DomainObject.IncrementValue();
                return DomainObject.Value;
            }
        }
    }
    

    [Application]

    private void button1_Click(object sender, EventArgs e)
    {
        AppDomain domain1 = AppDomain.CreateDomain("domain1");
        AppDomain domain2 = AppDomain.CreateDomain("domain2");
    
        DomainObject object1 = 
            domain1.CreateInstanceAndUnwrap("MyLibrary", "MyLibrary.DomainObject") 
            as DomainObject;
    
        DomainObject object2 = 
            domain2.CreateInstanceAndUnwrap("MyLibrary", "MyLibrary.DomainObject") 
            as DomainObject;
    
        if (object1 != null)
        {
            Console.WriteLine("object 1 Value = " 
                              + object1.GetIncrementedValue().ToString());
            Console.WriteLine("object 1 Value = " 
                              + object1.GetIncrementedValue().ToString());
            Console.WriteLine("object 1 Value = " 
                              + object1.GetIncrementedValue().ToString());
        }
        if (object2 != null)
        {
            Console.WriteLine("object 2 Value = "
                              + object2.GetIncrementedValue().ToString());
            Console.WriteLine("object 2 Value = "
                              + object2.GetIncrementedValue().ToString());
            Console.WriteLine("object 2 Value = "
                              + object2.GetIncrementedValue().ToString());
        }
    
        /* Unload the Domain and re-create
         * This should reset the Static Value in the AppDomain
         */
        AppDomain.Unload(domain1);
        domain1 = AppDomain.CreateDomain("domain1");
        object1 = domain1.CreateInstanceAndUnwrap("MyLibrary", 
                                                  "MyLibrary.DomainObject") 
                                                  as DomainObject;
    
        if (object1 != null)
        {
            Console.WriteLine("object 1 Value = "
                              + object1.GetIncrementedValue().ToString());
            Console.WriteLine("object 1 Value = "
                              + object1.GetIncrementedValue().ToString());
            Console.WriteLine("object 1 Value = "
                              + object1.GetIncrementedValue().ToString());
        }
        if (object2 != null)
        {
            Console.WriteLine("object 2 Value = "
                              + object2.GetIncrementedValue().ToString());
            Console.WriteLine("object 2 Value = "
                              + object2.GetIncrementedValue().ToString());
            Console.WriteLine("object 2 Value = "
                              + object2.GetIncrementedValue().ToString());
        }
    }
    

    Generated Results:

    object 1 Value = 1
    object 1 Value = 2
    object 1 Value = 3
    object 2 Value = 1
    object 2 Value = 2
    object 2 Value = 3
    object 1 Value = 1
    object 1 Value = 2
    object 1 Value = 3
    object 2 Value = 4
    object 2 Value = 5
    object 2 Value = 6
    
    0 讨论(0)
提交回复
热议问题