gpt4 book ai didi

c# - 使用 MarshalByRefObject 跨应用域传递数据

转载 作者:行者123 更新时间:2023-11-30 14:04:31 26 4
gpt4 key购买 nike

我在两个 .NET 应用程序域之间传递一些数据时遇到了一些问题,我希望这里有人可以帮助我。

基本上我拥有的是一个主应用程序 (Main),它将程序集 AB 加载到它的主域中,然后当我运行插件(C) Main 调用B 上的创建域方法,它创建一个新域并加载CB 的实例放入其中,这样 C 只能访问 B 而不能访问其他的。

B 包含指向 Main 的 IDispatch 的指针,但它似乎只有在使用 C 加载到新域后才能获取它.我想要做的是从 B 的新域实例发送指针的副本,并将其发送到仍在默认域中运行的 A

郑重声明,我控制了 A、B 和 C 但不控制 Main

抱歉,如果这有点难以理解,我已尽力解释。

代码:

在 A 中:

public class Tunnel : MarshalByRefObject
{
public void SetPointer(int dispID)
{
IntPtr pointer = new IntPtr(dispID);
}
}

在 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.)

}

最后看起来像这样:

默认域:

  • Main.dll
  • A.dll
  • B.dll

插入域:

  • B.dll
  • C.dll

最佳答案

在你上面的代码中你正在调用

AppDomain.CurrentDomain.CreateInstanceAndUnwrap(...)

这只是在当前域中创建对象的一种迂回方式,就像您刚刚调用构造函数一样。您需要在远程域上调用该方法,即。

AppDomain domain = AppDomain.Create(...)
Tunnel tunnel = (Tunnel)domain.CreateInstanceAndUnwrap(...)

如果您随后调用将在远程对象上运行的 tunnel.SetPointer(...)。

关于c# - 使用 MarshalByRefObject 跨应用域传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1767439/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com