dimanche 22 septembre 2019

Converting an anonymous type to a dictionary

I am attempting to create a function that converts an anonymous type to a dictionary. I was going over the accepted answer in this link thread. However I am getting the error

"Cannot use lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type"

This is what I am trying to do

public Dictionary<string,string> convert(dynamic dtype)
{
 var props = content.GetType().GetProperties();
 var pairs = props.Select(x => x.Name + "=" + x.GetValue(a, null)).ToArray();  // <----Exception
 var result = string.Join("&", pairs);
 return result
} 

Any suggestion on how I can fix this ? I am trying to do this

       var student= new
        {
            // TODO: Make this figure out and return underlying device status.
            type = "Active",
        };



 var dict = convert(student);





Aucun commentaire:

Enregistrer un commentaire