gpt4 book ai didi

c# - 创建子应用程序域的对象是否在该子应用程序域中实例化?

转载 作者:行者123 更新时间:2023-11-30 21:21:13 24 4
gpt4 key购买 nike

创建子应用域的对象是否在该子应用域中实例化?

我在主 AppDomain 中有一个对象,它正在创建另一个 AppDomain,它要求调用类是可序列化的,并在新的子 AppDomain 中创建调用类的实例。

我想知道是不是这样,或者是否有一种方法可以创建子 appDomain 但仍然保留主 appDomain 中调用对象的原始实例化

最佳答案

没有。

您在代码中执行的某些操作导致对象被拉过域边界。

//the current class is creating a domain.  No types exist in the domain
var domain = AppDomain.CreateDomain("2nd Domain");

// create an instance of SomeType in 2nd Doman and create a proxy here
var assembly = typeof(SomeType).Assembly.FullName;
var type = typeof(SomeType).Name;
var proxy = domain.CreateInstanceAndUnwrap(assembly,type);
// at this point, only one instance exists in 2nd Domain.

//These will cause the current class to be pulled across the domain boundary
proxy.Whoops(this);
proxy.SomeEvent += new EventHandler(AMethodDefinedHere);
proxy.Callback = AnotherMethodDefinedHere;

只有当您将指向当前实例的指针交给代理(并且代理使用它)时,该实例才会被拉过边界。

您的代理应该只获取和返回原始类型(如字符串或字节或此类数组)或您定义的可序列化或扩展 MarshalByRefObject 的密封类型。

关于c# - 创建子应用程序域的对象是否在该子应用程序域中实例化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2876947/

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