Mat Walker
Mat Walker

Reputation: 105

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(); but am getting a RuntimeBinderException for the ObjectHandle casting and I don't know why.

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'

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). Any ideas?

Upvotes: 0

Views: 502

Answers (1)

Nikhil Vartak
Nikhil Vartak

Reputation: 5117

Why to go a complicated way!? Just do:

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

Upvotes: 1

Related Questions