gpt4 book ai didi

c# - 解决方法 .net 应用程序域仅按值传递对象

转载 作者:行者123 更新时间:2023-12-02 14:46:17 25 4
gpt4 key购买 nike

我正在开发一个严重依赖插件的.net应用程序。应用程序本身包含到远程服务器的连接。

最近,我深入研究了应用程序域,并将它们视为将插件代码与应用程序其余部分隔离的理想解决方案。

但是,有一个很大的缺点,使我无法实现用于托管插件的应用程序域。似乎没有办法通过引用另一个应用程序域来传递对象,而另一个应用程序域需要传递对连接对象的引用。

我希望有人能给我一个解决方法,以便我可以传递对该对象的引用。

注意:创建代理是不可能的,连接层已经充当代理,因为类是自动生成的。

注意2:不能使用System.AddIn,因为它在紧凑框架上不可用。

最佳答案

您是否尝试过从 MarshalByRefObject 派生?这是一个痛苦,因为它搞乱了你的继承层次结构,但我认为这就是你想要的。

来自文档:

MarshalByRefObject is the base class for objects that communicate across application domain boundaries by exchanging messages using a proxy. Objects that do not inherit from MarshalByRefObject are implicitly marshal by value. When a remote application references a marshal by value object, a copy of the object is passed across application domain boundaries.

MarshalByRefObject objects are accessed directly within the boundaries of the local application domain. The first time an application in a remote application domain accesses a MarshalByRefObject, a proxy is passed to the remote application. Subsequent calls on the proxy are marshaled back to the object residing in the local application domain.

Types must inherit from MarshalByRefObject when the type is used across application domain boundaries, and the state of the object must not be copied because the members of the object are not usable outside the application domain where they were created.

根据我的经验,它可能非常有限 - 您确实需要在 AppDomain 边界上做尽可能少的事情,最好将自己限制在只需要原始类型、字符串和数组的操作上。这很可能是由于我自己在使用多个 AppDomain 方面缺乏经验,但这只是一个警告,表明它有点雷区。

关于c# - 解决方法 .net 应用程序域仅按值传递对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/323212/

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