简体   繁体   English

是否可以判断对象是否在不同的AppDomain中运行?

[英]Is it possible to tell if an object is running in a different AppDomain?

I want to know if I can tell what appdomain a object was created in. This is for a unit test but also useful general knowledge. 我想知道我是否可以告诉创建对象的appdomain。这是一个单元测试,但也是有用的常识。 I have the following pieces of code (this is example code for illustration). 我有以下几段代码(这是用于说明的示例代码)。

public Foo Create()
{
    AppDomainSetup appDomainSetup = 
        new AppDomainSet { ApplicationBase = @"z:\SomePath" }

    AppDomain appDomain = 
                  AppDomain.CreateDomain("DomainName", null, appDomainSetup);

    return (Foo) appDomain.CreateInstanceAndUnwrap("MyAssembly", "MyClass");
}

I then call 然后我打电话

Foo myFoo = Create();

What I would like to be able to do is find out what AppDomain method on myFoo will be called in, to test that the Create method had actually created a new AppDomain. 我想要做的是找出myFoo上的AppDomain方法将被调用,以测试Create方法实际上是否已经创建了一个新的AppDomain。 I realise that I can add a method on Foo like 我意识到我可以在Foo上添加一个方法

public class Foo
{
    public string appDomainName 
    { 
        get { return AppDomain.CurrentDomain.FriendlyName; } 
    }
}

This would provide me the appdomain that Foo is running in. I don't think this is an elegant solution just for a unit test. 这将为我提供Foo运行的应用程序。我认为这不仅仅是单元测试的优雅解决方案。 It would be great if someone could help define a method like. 如果有人可以帮助定义类似的方法,那将会很棒。

public string GetAppDomainNameWithDotNetWitchcraft(Foo myFoo)
{
    // Insert voodoo here.
}

EDIT: Thanks for the responses and comments. 编辑:感谢您的回复和评论。 The question I have asked has been answered and the comments have helped me realised where I was going wrong. 我问过的问题已经得到了解答,这些评论帮助我意识到我的错误。 What I really was trying to achieve is to test that a new AppDomain is created. 我真正想要实现的是测试是否创建了一个新的AppDomain。

Well, you can do a bit of Spelunking via Remoting/Reflection, assuming your running in full trust. 好吧,你可以通过Remoting / Reflection做一些Spelunking,假设你完全信任。 Note that you have to access a private property, and this assumes that the only thing it can find is remoting due to cross app domains: 请注意,您必须访问私有属性,并且这假定它可以找到的唯一内容是由于跨应用程序域而进行远程处理:

    var a = Create();
    if (System.Runtime.Remoting.RemotingServices.IsTransparentProxy(a))
    {
        var c = System.Runtime.Remoting.RemotingServices.GetObjRefForProxy(a);
        var ad = c.ChannelInfo.ChannelData[0];
        var propDomainId = ad.GetType().GetProperty("DomainID", BindingFlags.NonPublic | BindingFlags.Instance);
        var DomainID = propDomainId.GetValue(ad,null);
    }

You can then compare that domain ID to your own to know if it's in your domain. 然后,您可以将该域ID与您自己的域名进行比较,以了解它是否在您的域中。 Mind you, it's unlikely you'll enter the if statement if it is in your domain (trying to think of circumstances where you'd have a transparent proxy to an object in your own domain). 你要知道,这是不可能的,你会进入if语句,如果它你的域(冥思苦想的情况下,您就会有一个透明代理对象在自己的域名)。

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

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