在appdomain之间来回传递集合对象 [英] Passing collection objects back and forth between appdomains

查看:105
本文介绍了在appdomain之间来回传递集合对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下示例基于"将值来回传递appdomains ",Marc Gravell在此很好地回答了有关appdomain之间的.Net远程处理的问题.我所做的就是(非常天真?)扩展了它,它也应该适用于字符串数组.

The following sample is based on "Passing values back and forth appdomains", where Marc Gravell kindly provided a very good answer to a question about .Net remoting between appdomains. What I've done is extended it in a (very naive?) expectation that it should also work for an array of strings.

问题在于,它只能以一种方式工作-创建的appdomain可以访问该数组,但只能以只读方式访问.我想要的也是将更新后的数组元素也恢复到原始的appdomain中.我什至想用List<>和Dictionary<>对象来做到这一点.这可能吗?

The problem is that it only works one way - the created appdomain can access the array, but only readonly. What I'd like is to get the updated array elements back in the original appdomain too. I'd even like to do this with List<> and Dictionary<> objects. Is this possible?

using System;

namespace StackoverflowSample
{
   class MyBoundaryObject : MarshalByRefObject
   {
      public void SomeMethod(AppDomainArgs ada)
      {
         Console.WriteLine(AppDomain.CurrentDomain.FriendlyName + "; executing");
         ada.MyString = "working!";
         ada.MyStringArray[0] = "working!";
         string s = ada.MyStringArray[0];  // s is assigned value "a"!!!
      }
   }


   public class AppDomainArgs : MarshalByRefObject
   {
      public string MyString { get; set; }
      public string[] MyStringArray { 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";
         ada.MyStringArray = new string[] { "a", "b" };
         Console.WriteLine("Before: " + ada.MyString + " " + ada.MyStringArray[0]);

         boundary.SomeMethod(ada);

         Console.WriteLine("After: " + ada.MyString + " " + ada.MyStringArray[0]);
         Console.ReadKey();
         AppDomain.Unload(domain);
      }
   }
}

推荐答案

有时候我也有这个要求,即将更新后的列表返回给Main AppDomain,我使用创建new instance of a Listassign the desired values的解决方法解决了它.这应该对您有用-

Sometime back i have this requirement too of returning the updated list back to Main AppDomain and i solved it using a workaround of creating a new instance of a List and assign the desired values. This should work for you -

ada.MyStringArray = new string[] { "working!", "b" };
string s = ada.MyStringArray[0];  // s will be assigned value "working!"!!!

更新

我想您必须clone实例并实例化new instance,然后才能从远程方法返回.简单stringworking的原因是-

I guess you have to clone the instance and instantiate a new instance before returning from remote method. Reasons for the which it is working for simple string is -

字符串是immutable,即每次您使用不同的值对其进行初始化时,都会在幕后为其创建new instance,有点像new String().因此,该更新在其他appDomain中可见.

Strings are immutable i.e. every time you initialize it with different value, a new instance is created for it behind the scenes somewhat like new String(). Hence, the update is visible in other appDomain.

我用StringBuilder尝试了这个小事情,它们是mutable,即,当您更改对象的内容时,不会为它们创建新实例.

I tried this small thing with StringBuilder which are mutable i.e new instance is not created for them when you change the content of the object.

public class AppDomainArgs : MarshalByRefObject
{
    public StringBuilder MyStringBuilder { get; set; }
}

public void SomeMethod(AppDomainArgs ada)
{
    Console.WriteLine(AppDomain.CurrentDomain.FriendlyName + "; executing");
    ada.MyString = "working!";
    ada.MyStringBuilder.Append(" working!");
}

现在,查看输出-

Console.WriteLine("Before: " + ada.MyString + " " + ada.MyStringArray[0] + " " + 
                      ada.MyStringBuilder);    
boundary.SomeMethod(ada);    
Console.WriteLine("After: " + ada.MyString + " " + ada.MyStringArray[0] + " "
                      ada.MyStringBuilder);

您将看到StringBuilder对象未更改.理想情况下,其值应为"a working!",但仍应为"a".

You will see that StringBuilder object is unchanged. Ideally, its value should be "a working!" but still the value is "a".

这篇关于在appdomain之间来回传递集合对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆