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.
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);
}
}
}