在应用程序域之间来回传递值
我有以下代码:
public class AppDomainArgs : MarshalByRefObject {
public string myString;
}
static AppDomainArgs ada = new AppDomainArgs() { myString = "abc" };
static void Main(string[] args) {
AppDomain domain = AppDomain.CreateDomain("Domain666");
domain.DoCallBack(MyNewAppDomainMethod);
Console.WriteLine(ada.myString);
Console.ReadKey();
AppDomain.Unload(domain);
}
static void MyNewAppDomainMethod() {
ada.myString = "working!";
}
我认为 make this 将使我的 ada.myString “工作!” 在主应用程序域上,但事实并非如此。 我认为通过继承 MarshalByRefObject 对第二个应用程序域所做的任何更改也会反映在原始应用程序域中(我认为这只是主应用程序域上真实对象的代理!)?
谢谢
I have the following code:
public class AppDomainArgs : MarshalByRefObject {
public string myString;
}
static AppDomainArgs ada = new AppDomainArgs() { myString = "abc" };
static void Main(string[] args) {
AppDomain domain = AppDomain.CreateDomain("Domain666");
domain.DoCallBack(MyNewAppDomainMethod);
Console.WriteLine(ada.myString);
Console.ReadKey();
AppDomain.Unload(domain);
}
static void MyNewAppDomainMethod() {
ada.myString = "working!";
}
I thought make this would make my ada.myString have "working!" on the main appdomain, but it doesn't. I thought that by inhering from MarshalByRefObject any changes made on the 2nd appdomain would reflect also in the original one(I thought this would be just a proxy to the real object on the main appdomain!)?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
代码中的问题是您从未真正将对象传递到边界上; 因此,您有两个
ada
实例,每个应用程序域中有一个实例(静态字段初始值设定项在两个应用程序域上运行)。 您需要将实例传递到边界,才能让MarshalByRefObject
魔法发挥作用。例如:
The problem in your code is that you never actually pass the object over the boundary; thus you have two
ada
instances, one in each app-domain (the static field initializer runs on both app-domains). You will need to pass the instance over the boundary for theMarshalByRefObject
magic to kick in.For example: