简体   繁体   English

获取对象的AppDomain

[英]Get the AppDomain of object

有没有办法确定哪个AppDomain是对象或ObjectHandle实例创建的?

If your object "travelled" using (eg) serialization from another AppDomain to the current AppDomain then it has essentially been "created" in your current AppDomain. 如果您的对象使用(例如)从另一个AppDomain序列化到当前AppDomain“旅行”,那么它基本上已在您当前的AppDomain中“创建”。 The source AppDomain could be a separate process on the current computer or another process on a remote computer. 源AppDomain可以是当前计算机上的单独进程,也可以是远程计算机上的其他进程。 As far as I am aware, I don't think that the CLR keeps track of that for you, since you are responsible for moving objects between processes. 据我所知,我不认为CLR会为您跟踪,因为您负责在进程之间移动对象。 You would probably need to add a field to your class so that you can set and get that information. 您可能需要在类中添加一个字段,以便设置和获取该信息。

Or consider using a LogicalCallContext object that tracks this information for you while travelling with a call accross appdomains. 或者考虑使用LogicalCallContext对象,在对应appdomains的呼叫旅行时为您跟踪此信息。 Here is a good blog by Jeffrey Richter about this. 是杰弗里里希特关于此的一个很好的博客。

An object from another app domain is a transparent proxy. 来自另一个应用程序域的对象是透明代理。 It is possible to get the real proxy and access the private field that contains the domain id: 可以获取真实代理并访问包含域ID的私有字段:

public static int GetObjectAppDomain(object proxy)
{
    RealProxy rp = RemotingServices.GetRealProxy(proxy);
    int id = (int)rp.GetType().GetField("_domainID", BindingFlags.Instance|BindingFlags.NonPublic).GetValue(rp);
    return id;
}

If the list of possible app domains isn't known, here is a way to get the list of all app domains. 如果可能的应用程序域的列表是不知道, 这里是一个办法让所有的应用程序域的列表。

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

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