简体   繁体   English

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

[英]does the object creating a sub appdomain get instantiated in that sub appdomain?

does the object creating a sub appdomain get instantiated in that sub appdomain? 创建子应用程序域的对象是否在该子应用程序域中实例化?

I have an object that is in the main AppDomain and it is creating another AppDomain and it requires the calling class to be serializable and is creating an instance of the calling class in the new sub AppDomain. 我在主AppDomain中有一个对象,它正在创建另一个AppDomain,它要求调用类可序列化,并在新的子AppDomain中创建调用类的实例。

I'm wondering if that is how it is, or if there is a way that I can create the sub appDomain but still hold on to the original instantiation of the calling object in the main appDomain 我想知道这是怎么回事,或者是否有一种方法可以创建子appDomain但仍保留主appDomain中调用对象的原始实例化

No. 没有。

You're doing something in your code that is causing the object to be pulled across the domain boundary. 您正在代码中执行某些操作,导致该对象被拉到域边界之外。

//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;

Only when you hand the proxy a pointer to the current instance (and the proxy uses it) does the instance get pulled across the boundary. 仅当您将代理的指针传递给当前实例(并且代理使用它)时,实例才会越过边界。

Your proxy should only take and return primitive types (like string or byte or arrays of such) or sealed types you define that are serializable or extend MarshalByRefObject. 您的代理只能接受并返回您定义的可序列化或扩展MarshalByRefObject的原始类型(如字符串或字节或此类数组)或密封类型。

MarshalByRefObject继承您的对象,并且无需序列化即可跨越应用程序域边界进行调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM