在应用程序域之间来回传递值

发布于 2024-07-30 11:29:58 字数 711 浏览 6 评论 0原文

我有以下代码:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

几味少女 2024-08-06 11:29:58

代码中的问题是您从未真正将对象传递到边界上; 因此,您有两个 ada 实例,每个应用程序域中有一个实例(静态字段初始值设定项在两个应用程序域上运行)。 您需要将实例传递到边界,才能让 MarshalByRefObject 魔法发挥作用。

例如:

using System;
class MyBoundaryObject : MarshalByRefObject {
    public void SomeMethod(AppDomainArgs ada) {
        Console.WriteLine(AppDomain.CurrentDomain.FriendlyName + "; executing");
        ada.myString = "working!";
    }
}
public class AppDomainArgs : MarshalByRefObject {
    public string myString { get; set; }
}
static class Program {
     static void Main() {
         AppDomain domain = AppDomain.CreateDomain("Domain666");
         MyBoundaryObject boundary = (MyBoundaryObject)
              domain.CreateInstanceAndUnwrap(
                 typeof(MyBoundaryObject).Assembly.FullName,
                 typeof(MyBoundaryObject).FullName);

         AppDomainArgs ada = new AppDomainArgs();
         ada.myString = "abc";
         Console.WriteLine("Before: " + ada.myString);
         boundary.SomeMethod(ada);
         Console.WriteLine("After: " + ada.myString);         
         Console.ReadKey();
         AppDomain.Unload(domain);
     }
}

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 the MarshalByRefObject magic to kick in.

For example:

using System;
class MyBoundaryObject : MarshalByRefObject {
    public void SomeMethod(AppDomainArgs ada) {
        Console.WriteLine(AppDomain.CurrentDomain.FriendlyName + "; executing");
        ada.myString = "working!";
    }
}
public class AppDomainArgs : MarshalByRefObject {
    public string myString { get; set; }
}
static class Program {
     static void Main() {
         AppDomain domain = AppDomain.CreateDomain("Domain666");
         MyBoundaryObject boundary = (MyBoundaryObject)
              domain.CreateInstanceAndUnwrap(
                 typeof(MyBoundaryObject).Assembly.FullName,
                 typeof(MyBoundaryObject).FullName);

         AppDomainArgs ada = new AppDomainArgs();
         ada.myString = "abc";
         Console.WriteLine("Before: " + ada.myString);
         boundary.SomeMethod(ada);
         Console.WriteLine("After: " + ada.myString);         
         Console.ReadKey();
         AppDomain.Unload(domain);
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文