我如何通过横跨应用程序域方法参数引用?应用程序、参数、方法

2023-09-03 04:56:46 作者:别出现在我梦里

我一直在试图让下面的code工作(一切都在同一个组件定义):

I have been trying to get the following code to work(everything is defined in the same assembly) :

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."
   }
}

}

我试图做的是首次通过AppDomain中的子域创建一个A实例的引用,并有子域的第一个域执行的方法。在对'B'code某些时候,我会打电话给remoteObj.GetSomeData()。这有许多工作要做,因为从GetSomeData'法'的byte []必须在第一个AppDomain的计算。   我应该怎么做,以避免该异常,或者我能做些什么来达到同样的效果?

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?

推荐答案

我可以复制的问题,它似乎与TestDriven.net和/或xUnit.net。如果我运行C.Init()作为一种测试方法,我得到同样的错误消息。不过,如果我从控制台应用程序运行C.Init(),我不明白的例外。

I can duplicate the issue, and it seems to be related to TestDriven.net and/or xUnit.net. If I run C.Init() as a test method, I get the same error message. However, if I run C.Init() from a console application, I do not get the exception.

您看到了同样的事情,从一个单元测试运行C.Init()?

Are you seeing the same thing, running C.Init() from a unit test?

编辑:我也能重复使用NUnit和TestDriven.net问题。我也能重复使用NUnit的亚军,而不是TestDriven.net的错误。所以,问题似乎与通过一个测试框架运行此code,虽然我不知道为什么。

I'm also able to duplicate the issue using NUnit and TestDriven.net. I'm also able to duplicate the error using the NUnit runner instead of TestDriven.net. So the problem seems to be related to running this code through a testing framework, though I'm not sure why.