【发布时间】:2018-06-04 05:07:25
【问题描述】:
我在 C# 库程序集中有以下类。
public static class TestClass
{
static int counter = 0;
static TestClass()
{
System.Diagnostics.Debug.Print("TestLibrary.TestClass is constructed...");
}
public static void Say()
{
System.Diagnostics.Debug.Print($"TestClass.Say called {counter++} times");
}
}
[Serializable]
public class TestLibraryProxy
{
static int _executionCount = 0;
public void Execute()
{
System.Diagnostics.Debug.Print($"TestLibraryProxy Execute [{_executionCount++}]");
TestClass.Say();
TestClass.Say();
}
}
我正在尝试从另一个 C# 应用程序将上述程序集加载到应用程序域并调用 TestLibraryProxy.Execute() 方法。
class Program
{
static void Main(string[] args)
{
CreateAppDomainAndCallStaticMethod("MyDomain1");
CreateAppDomainAndCallStaticMethod("MyDomain2");
CreateAppDomainAndCallStaticMethod("MyDomain3");
CreateAppDomainAndCallStaticMethod("MyDomain4");
}
private static void CreateAppDomainAndCallStaticMethod(string domainName)
{
AppDomain domain = AppDomain.CreateDomain(domainName);
var k = domain.CreateInstanceAndUnwrap("TestLibrary", typeof(TestLibraryProxy).FullName) as TestLibraryProxy;
k.Execute();
AppDomain.Unload(domain);
}
}
Output
TestLibraryProxy Execute [0]
TestLibrary.TestClass is constructed...
TestClass.Say called 0 times
TestClass.Say called 1 times
TestLibraryProxy Execute [1]
TestClass.Say called 2 times
TestClass.Say called 3 times
TestLibraryProxy Execute [2]
TestClass.Say called 4 times
TestClass.Say called 5 times
TestLibraryProxy Execute [3]
TestClass.Say called 6 times
TestClass.Say called 7 times
我注意到 TestClass 中的静态计数器值在应用程序域中得到保留。即使我多次加载和卸载应用程序域,此值也会保留。
我已经读到静态变量存储在应用程序域级别。但看起来不是真的?知道 CLR 如何跨应用程序域管理静态数据。
【问题讨论】:
-
这对@Vijay 有帮助吗?