无法强制转换System.Runtime.Remoting.ObjectHandle

编程入门 行业动态 更新时间:2024-10-21 14:32:59
本文介绍了无法强制转换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

本文发布于:2023-11-10 02:25:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574087.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Runtime   System   ObjectHandle   Remoting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!