我在一个解决方案中有两个项目(将它们视为项目 1 和项目 2)。第一个项目 (project1) 包含一个保存全局变量的类
public static class GlobalTrackInfo
{
public static string tracktitle { get; set; }
public static Uri trackUri { get; set; }
}
我在 project2 中添加了 project1 的引用,并在 project2 的类中如下设置静态变量的值
GlobalTrackInfo.tracktitle = "myTitle";
GlobalTrackInfo.trackUri = new Uri("www.example.com");
后来我尝试访问project1类中的这些变量作为
Title = GlobalTrackInfo.tracktitle;
但它似乎是空的。我究竟做错了什么? 不能跨不同项目使用全局变量吗?
最佳答案
当两个项目拥有自己的 AppDomain 时,您不能在它们之间共享静态变量。根据 MSDN 文档:
By default, each process using a DLL has its own instance of all the DLLs global and static variables..
如果您想这样做,最好使用其他方法,例如网络通信 (IPC) 或类似的方法。
关于c# - 静态变量变为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35290992/