2010-05-20 26 views
2

創建子appdomain的對象是否在該子appdomain中實例化?創建子appdomain的對象是否在該子appdomain中實例化?

我有一個對象,它是在主AppDomain中,它是創建另一個AppDomain中,它需要調用的類可序列化,並在新的子AppDomain中是創建調用類的一個實例。

我想如果這是怎麼回事,或者如果我可以創建子的AppDomain,但仍然堅持到調用對象的原始實例主的AppDomain

回答

1

繼承的方式你對象從MarshalByRefObject,你不需要序列化它來跨應用程序域的邊界進行調用。

+0

謝謝,但真的我想知道創建子appdomain的對象是否在子appdomain中實例化,並且爲什麼 – Eric 2010-05-20 19:37:07

+0

@Eric:很可能是,並且因爲您在應用程序域中的該對象上調用了一個方法。如果您試圖在應用程序域中調用某個方法,並且該方法是實例範圍的,那麼必須引用實例所關聯的實例。如果通過引用對象使對象成爲編組,則在其他應用程序域中創建的引用基本上是主應用程序域中原始代理的代理。儘管沒有代碼,但是不可能給出確切的答案;特別是在「爲什麼」部分。 – 2010-05-20 19:41:15

3

你做你的代碼的東西,導致該對象#跨越域邊界拉。

//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的基本類型(如字符串或字節或這樣的數組)。

相關問題