I am trying to convert an Object to dynamic type but the conversion is failing with RunTimeBinder exception
Asked Answered
B

4

16

I am trying to convert an Object to dynamic type but the conversion is failing with RunTimeBinder exception. I tried using two methods that I came across in Stackoverflow answers.

Code 1:

object objSum;
dynamic dynSum;
objSum = dataTableColumnChart.Compute(String.Format("Count({0})", strColumnName), "");
dynSum = Convert.ChangeType(objSum, objSum.GetType());\
Debug.Writeline(dynSum);

Code 2:

dynSum=objSum;
Debug.Writeline(dynSum);

The exception thrown is this:

A first chance exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in Unknown Module.

Please note that in both cases exception is thrown when Debug statement is executed.

Baskett answered 22/3, 2016 at 6:55 Comment(8)
Are you sure you get the same error with code 2? I don't think that a simple assignment to a dynamic type will cause an exception. Can you give the exact error message?Watchtower
@Watchtower Hi, I have added the details in my question now.Baskett
is an exception thrown with Debug.Writeline(dynSum.ToString());Kincaid
A "first chance" exception is nothing unusual when you use the dynamic keyword. You just see the DLR probing for ways to evaluate an expression, the quickest way to do so is just try it and catch the exception if its approach did not pan out. Feature, not a bug.Sociopath
@Kincaid Yes. It throws the same exception.Baskett
blog.jorgef.net/2011/06/converting-any-object-to-dynamic.htmlKincaid
In the future, add the exception message and the line of code where the exception was thrown. It saves everyone a lot of time, and you'll get a better answer sooner. As is often the case, all the information you need is in the exception data you've thrown away :)Flameout
Sure. Thanks for the suggestion:)Baskett
R
4

The exception is:

Cannot dynamically invoke method 'Write' because it has a Conditional attribute

And when you check possible Debug.WriteLine inputs, "dynamic" is not one of them. So you need to cast it, to string for example:

    string strForWriteLine = dynSum.ToString() as string;
    Debug.WriteLine(strForWriteLine);

Hope this helps

*Edit: A little bit detail about dynSum.ToString() as string; When you just use ToString() you still get a dynamic string.

var strForWriteLine = dynSum.ToString();

strForWriteLine's type is dynamic { string }

Ribose answered 22/3, 2016 at 7:35 Comment(1)
Hi, it is working now. Thanks! So the conversion was happening fine and problem was with Debug,Writeline. Thanks for the explanation.Baskett
U
31

Here is extension method to convert an object to Dynamic

public static dynamic ToDynamic(this object value)
    {
        IDictionary<string, object> expando = new ExpandoObject();

        foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(value.GetType()))
            expando.Add(property.Name, property.GetValue(value));

        return expando as ExpandoObject;
    }
Underpass answered 26/6, 2016 at 20:42 Comment(0)
B
14

you should use JsonConvert. Fist of all, Serialize object to string, then Deserialize string to dynamic.

string str = JsonConvert.SerializeObject(objectstring);
dynamic obj = JsonConvert.DeserializeObject(str);
Balliol answered 12/9, 2017 at 2:33 Comment(2)
or objectstring.ToString() directly in DeserializeObjectBerrie
This only works if all of your properties are JSON-serializable. This is a big if.Radiate
R
4

The exception is:

Cannot dynamically invoke method 'Write' because it has a Conditional attribute

And when you check possible Debug.WriteLine inputs, "dynamic" is not one of them. So you need to cast it, to string for example:

    string strForWriteLine = dynSum.ToString() as string;
    Debug.WriteLine(strForWriteLine);

Hope this helps

*Edit: A little bit detail about dynSum.ToString() as string; When you just use ToString() you still get a dynamic string.

var strForWriteLine = dynSum.ToString();

strForWriteLine's type is dynamic { string }

Ribose answered 22/3, 2016 at 7:35 Comment(1)
Hi, it is working now. Thanks! So the conversion was happening fine and problem was with Debug,Writeline. Thanks for the explanation.Baskett
K
1

Try the following:

dynSum = objSum;
Kincaid answered 22/3, 2016 at 7:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.