title: How do I pass references as method parameters across AppDomains?
body: <p>I have been trying to get the following code to work(everything is defined in the same assembly) :</p>
  
  <pre><code>namespace SomeApp{
  
  public class A : MarshalByRefObject
  {
     public byte[] GetSomeData() { // }
  }
  
  public class B : MarshalByRefObject
  {
     private A remoteObj;
  
     public void SetA(A remoteObj)
     {
        this.remoteObj = remoteObj;
     }
  }
  
  public class C
  {
     A someA = new A();
     public void Init()
     {
         AppDomain domain = AppDomain.CreateDomain("ChildDomain");
         string currentAssemblyPath = Assembly.GetExecutingAssembly().Location;
         B remoteB = domain.domain.CreateInstanceFromAndUnwrap(currentAssemblyPath,"SomeApp.B") as B;
         remoteB.SetA(someA); // this throws an ArgumentException "Object type cannot be converted to target type."
     }
  }
  
  }
  </code></pre>
  
  <p>What I'm trying to do is pass a reference of an 'A' instance created in the first AppDomain to the child domain and have the child domain execute a method on the first domain. In some point on 'B' code I'm going to call 'remoteObj.GetSomeData()'. This has to be done because the 'byte[]' from 'GetSomeData' method must be 'calculated' on the first appdomain.
    What should I do to avoid the exception, or what can I do to achieve the same result?</p>