无法强制转换System.Runtime.Remoting.ObjectHandle [英] Unable to cast System.Runtime.Remoting.ObjectHandle

查看:907
本文介绍了无法强制转换System.Runtime.Remoting.ObjectHandle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我有一个接口 - 让我们说它调用 InterfaceName ,其实现称为 InterfaceImpl 。现在,当我动态尝试使用以下代码获取 InterfaceImpl

  object obj = Activator.CreateInstance(ProjectName,ProjectName.Folder.InterfaceImpl); 
InterfaceName in =(InterfaceName)obj; // Error pop here here

我收到以下错误

 无法转换类型为System.Runtime.Remoting.ObjectHandle的对象来键入ProjectName.Folder.InterfaceName。有关可能出现问题的任何建议吗?


h2_lin>解决方案

如果您阅读了有关所调用方法的文档,它会返回


必须打开才能访问新创建的实例的句柄。


查看 ObjectHandle 的文档,您只需调用 Unwrap(),以获取您尝试创建的类型的实例。



所以,我想你的真正问题是...为什么?



此方法设计为在另一个 AppDomain ,并且句柄返回到调用 AppDomain ,其中实例的代理是unwrapped。



什么?这不解释为什么?



只有两种类型可以跨越 AppDomain 障碍。可序列化(创建副本)的类型,以及扩展类型 MarshalByRefObject (其中创建并传递 代理)。 ObjectHandle 扩展 MarshalByRefObject ,因此可以跨越 AppDomain ,而它们表示的类型可能不能扩展MBRO 是可序列化的。这个方法可以确保你可以跨越障碍获取类型实例,无论什么。



所以,如果你只是试图实例化类型,你可能想看看不同的CreateInstance重载。

  var obj = Activator.CreateInstance(A,A.B.C)as ObjectHandle; 
InterfaceName in =(InterfaceName)obj.Unwrap();


In my code I have an interface - lets say its called InterfaceName and its implementation called InterfaceImpl. Now when I dynamically try to obtain the InterfaceImpl using the following code:

object obj = Activator.CreateInstance("ProjectName","ProjectName.Folder.InterfaceImpl");
InterfaceName in = (InterfaceName)obj; //Error pops up here

I get the following error

Unable to cast object of type 'System.Runtime.Remoting.ObjectHandle' to type 'ProjectName.Folder.InterfaceName'.

Any suggestions on what might be going wrong ?

解决方案

If you read the documentation about the method you are calling, it returns

A handle that must be unwrapped to access the newly created instance.

Looking at the documentation of ObjectHandle, you simply call Unwrap() in order to get the instance of the type you are trying to create.

So, I guess your real issue is... Why?

This method is designed to be called in another AppDomain, and the handle returned back to the calling AppDomain, where the proxy to the instance is "unwrapped".

What? That doesn't explain why?

Only two types can cross an AppDomain barrier. Types that are serializable (of which copies are created), and types that extend MarshalByRefObject (of which proxies are created and passed). ObjectHandle extends MarshalByRefObject, and so can cross that AppDomain barrier, whereas the type which they are representing may not extend MBRO or be serializable. This method ensures you can get that type instance across the barrier, no matter what.

So, if you are just trying to instantiate a type, you might want to look at a different overload of CreateInstance. Or just unwrap the result.

var obj = Activator.CreateInstance("A","A.B.C") as ObjectHandle;
InterfaceName in = (InterfaceName)obj.Unwrap(); 

这篇关于无法强制转换System.Runtime.Remoting.ObjectHandle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆