简体   繁体   English

动态转换为ObjectHandle会引发RuntimeBinderException

[英]Casting dynamic to ObjectHandle throws RuntimeBinderException

I'm trying to determine the type of a dynamic at run-time using the much publicized way of Type obtainedObjectType = ((ObjectHandle)obtainedDynamicObject).Unwrap().GetType(); 我正在尝试使用广为人知的Type obtainedObjectType = ((ObjectHandle)obtainedDynamicObject).Unwrap().GetType();来确定运行时动态Type obtainedObjectType = ((ObjectHandle)obtainedDynamicObject).Unwrap().GetType(); but am getting a RuntimeBinderException for the ObjectHandle casting and I don't know why. 但是正在为ObjectHandle转换获取RuntimeBinderException,我不知道为什么。

The following also throws the same error; 以下也引发相同的错误;

string myString = "ASF";
dynamic myDynamic = myString;
ObjectHandle dd = (ObjectHandle)myDynamic;

Throws: "Cannot convert type 'string' to 'System.Runtime.Remoting.ObjectHandle' 抛出:“无法将类型'string'转换为'System.Runtime.Remoting.ObjectHandle'

Odd thing is that I've used it at my last workplace and it was fine! 奇怪的是,我在上一个工作场所使用过它,很好! Using .NET framework 4.5 (and tried with later). 使用.NET Framework 4.5(并在以后进行尝试)。 Any ideas? 有任何想法吗?

Why to go a complicated way!? 为什么要走复杂的路! Just do: 做就是了:

Type myDynamicType = myDynamic.GetType();
Console.WriteLine(myDynamicType); // Output is System.String

暂无
暂无

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

相关问题 事件回调<dynamic>抛出 RuntimeBinderException</dynamic> - EventCallback<dynamic> throws RuntimeBinderException 动态属性分配引发RuntimeBinderException - Dynamic Property Assignment Throws RuntimeBinderException 为什么“((dynamic)dictionary).MyKey”抛出RuntimeBinderException? - Why “((dynamic) dictionary).MyKey” throws a RuntimeBinderException? 在嵌套在子文件夹中的Partial中使用@model dynamic会引发RuntimeBinderException - Using @model dynamic in a Partial nested in a subfolder throws RuntimeBinderException 为什么“动态”ExpandoObject 即使包含属性定义也会抛出 RuntimeBinderException? - Why 'dynamic' ExpandoObject throws RuntimeBinderException even if it contains the definition for a property? C#动态-“ RuntimeBinderException” - C# Dynamic - “RuntimeBinderException” 从继承的接口调用Method时传递动态参数会引发RuntimeBinderException - Passing a dynamic parameter throws RuntimeBinderException when calling Method from Inherited interface 将C#动态与COM对象一起使用会引发RuntimeBinderException,以记录已实现接口的方法 - Using C# dynamic with COM object throws RuntimeBinderException for documented method of implemented interface RuntimeBinderException 与 MVC 中的动态匿名对象 - RuntimeBinderException with dynamic anonymous objects in MVC 拦截动态调用以避免RuntimeBinderException - Intercept a dynamic call to avoid RuntimeBinderException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM