Nat*_*n W 3 .net c# appdomain marshalbyrefobject
我在两个.NET应用程序域之间传递一些数据时遇到了一些麻烦,我希望有人可以帮助我.
基本上我所拥有的是一个主应用程序(Main),它将程序集A和B加载到它的主域中,然后当我运行一个插件时(C)Main在B上调用一个创建域方法,它创建一个新域并加载C和一个实例将B放入其中,以便C只能访问B而不能访问其他人.
B包含一个指向Main的IDispatch的指针,但只有它在用C加载到新域后才会得到它.我要做的是从B的新域实例发送指针的副本,并将其发送到仍在默认域中运行的A.
只是为了记录我控制A,B和C但不控制主要
对不起,如果这有点难以理解,我尽力解释.
码:
在一个:
public class Tunnel : MarshalByRefObject
{
public void SetPointer(int dispID)
{
IntPtr pointer = new IntPtr(dispID);
}
}
Run Code Online (Sandbox Code Playgroud)
在B:
//Call by Main after loading plug in but after A.dll is loaded.
public void CreateDomain()
{
AppDomain maindomain= AppDomain.CurrentDomain;
tunnel = (Tunnel)maindomain.CreateInstanceAndUnwrap(typeof(Tunnel).FullName,
typeof(Tunnel).FullName);
AppDomain domain = base.CreateDomain(friendlyName, securityInfo, appDomainInfo);
//Load assembly C (plug in) in domain.
// C uses B so it loads a new instance of B into the domain also at the same time.
// If I do this here it creates new instance of A but I need to use the one in
// the main domain.
//tunnel = (Tunnel)domain.CreateInstanceAndUnwrap(typeof(Tunnel).FullName,
typeof(Tunnel).FullName);
tunnel.SetPointer(//Send data from B loaded in new domain.)
}
Run Code Online (Sandbox Code Playgroud)
所以最后它看起来像这样:
默认域名:
插件域名:
在你上面的代码中,你正在打电话
AppDomain.CurrentDomain.CreateInstanceAndUnwrap(...)
Run Code Online (Sandbox Code Playgroud)
这只是在当前域中创建对象的一种循环方式,就像刚刚调用构造函数一样.您需要在远程域上调用该方法,即.
AppDomain domain = AppDomain.Create(...)
Tunnel tunnel = (Tunnel)domain.CreateInstanceAndUnwrap(...)
Run Code Online (Sandbox Code Playgroud)
如果您随后调用将在远程对象上运行的tunnel.SetPointer(...).
| 归档时间: |
|
| 查看次数: |
9132 次 |
| 最近记录: |