我认为您可以使用MarshalByRefObject
包装类来完成这项工作。
考虑以下示例:
using System;
using System.IO;
using System.Reflection;
namespace ConsoleApplication13
{
class Program
{
static void Main(string[] args)
{
AppDomain appDomain = AppDomain.CreateDomain("foo");
FooFactory fooFactory = (FooFactory)appDomain.CreateInstanceFromAndUnwrap(Assembly.GetEntryAssembly().Location, typeof(FooFactory).FullName);
IFoo fooFromOtherDomain = fooFactory.CreateMeAFoo();
string message = "Hello World!";
Console.WriteLine("Data = {0} on AppDomain ID = {1}", message, AppDomain.CurrentDomain.Id);
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(message);
fooFromOtherDomain.Post(buffer);
}
}
public interface IFoo
{
void Post(byte[] data);
}
public abstract class FooBase
{}
/// <summary>
/// This class represents your class that can't be marshaled by ref...
/// </summary>
public class Foo : FooBase, IFoo, IDisposable
{
private MemoryStream _buffer;
public Foo()
{
this._buffer = new MemoryStream();
}
public void Post(byte[] data)
{
if (data == null)
throw new ArgumentNullException("data");
this._buffer.Seek(0, SeekOrigin.End);
this._buffer.Write(data, 0, data.Length);
OnNewData();
}
private void OnNewData()
{
string dataString = System.Text.Encoding.UTF8.GetString(this._buffer.ToArray());
Console.WriteLine("Data = {0} on AppDomain ID = {1}", dataString, AppDomain.CurrentDomain.Id);
}
public void Dispose()
{
this._buffer.Close();
}
}
/// <summary>
/// Wraps the non-remote Foo class and makes it remotely accessible.
/// </summary>
public class FooWrapper : MarshalByRefObject, IFoo
{
private IFoo _innerFoo;
public FooWrapper(IFoo innerFoo)
{
this._innerFoo = innerFoo;
}
public void Post(byte[] data)
{
this._innerFoo.Post(data);
}
}
/// <summary>
/// For demo purposes to get an instance of IFoo from the other domain.
/// </summary>
public class FooFactory : MarshalByRefObject
{
public IFoo CreateMeAFoo()
{
Foo foo = new Foo();
FooWrapper fooWrapper =new FooWrapper(foo);
return fooWrapper;
}
}
}
不要太在意 FooFactory 类。那只是为了让远程创建一个展示您的场景的对象变得容易。
本质上为您的远程类定义一个接口,例如IFoo
.
public interface IFoo
{
void Post(byte[] data);
}
创建一个派生自并实现的Foo
包装类MarshalByRefObject
IFoo
/// <summary>
/// Wraps the non-remote Foo class and makes it remotely accessible.
/// </summary>
public class FooWrapper : MarshalByRefObject, IFoo
{
private IFoo _innerFoo;
public FooWrapper(IFoo innerFoo)
{
this._innerFoo = innerFoo;
}
public void Post(byte[] data)
{
this._innerFoo.Post(data);
}
}
将您的 IFoo 实现传递给FooWrapper
.
Foo foo = new Foo();
FooWrapper fooWrapper = new FooWrapper(foo);
然后像往常一样返回FooWrapper
到另一个域并调用IFoo
方法。Post
该程序的执行输出:
