0

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?

1 Answer 1

1

Why to go a complicated way!? Just do:

Type myDynamicType = myDynamic.GetType();
Console.WriteLine(myDynamicType); // Output is System.String
Sign up to request clarification or add additional context in comments.

1 Comment

Was probably the first hit in a Google search lol... Tried your example and, obviously, it worked perfectly. Not sure why using the cast to ObjectHandle is being recommended when using the GetType() method works fine... Thanks Nikhilvartak

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.