静态变量变为空

Static variables becoming null

我在一个解决方案中有两个项目(将它们视为项目 1 和项目 2)。第一个项目 (project1) 包含一个包含全局变量

的 class
public static class GlobalTrackInfo
{
    public static string tracktitle { get; set; }
    public static Uri trackUri { get; set; }
}

我在 project2 中添加了 project1 的引用,并在 project2

的 class 中为静态变量设置了如下值
GlobalTrackInfo.tracktitle = "myTitle";
GlobalTrackInfo.trackUri   = new Uri("www.example.com");

后来我尝试在 project1 的 class 中访问这些变量作为

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) 或类似的方法。