开发者

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 t开发者_JS百科he 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.

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


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.


Inherit your object from MarshalByRefObject and you won't need to serialize it to make calls across application domain boundaries.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜