Is there a way to determine whether a TransparentProxy
is pointing to a valid reference?
I have IPlugin
. I create a new AppDomain, load the assembly with an implementation of IPlugin
, and 开发者_高级运维create an instance of that implemenation. I receive a IPlugin
, but under the covers its TransparentProxy
. If I unload the secondary AppDomain, the instance of IPlugin
(the one that the proxy points to) is gone. But the proxy is still pointing there. My program crashes (with no exceptions) when I try to access the proxy.
Psudeocode:
var domain = CreateDomain("domain");
var assembly = domain.LoadAssembly("myAssembly");
var plugin = domain.CreateObject("MyPlugin") as IPlugin;
// plugin is really a TransparentProxy to a MyPlugin
if (plugin != null)
plugin.DoSomething("123");
UnloadDomain(domain);
if (plugin != null) // Still evaluates to TRUE!
plugin.DoSomething("123"); // Program crashes with no exceptions
Well, since no one has suggested a proper answer, you could try this:
public static bool IsValidReference(MarshalByRefObject obj)
{
try {
obj.Equals(null);
return true;
} catch (RemotingException e) {
return false;
}
}
精彩评论