vendredi 7 avril 2017

Expression tree create dictionary with property values for class

Essentially I'm trying to do this using expression trees

var properties = new Dictionary<string, object>();

foreach (var propInfo in objType.GetTypeInfo().GetProperties(BindingFlags.Public))
{
    var name = propInfo.Name;
    var value = propInfo.GetValue(objInstance);

    properties.Add(name, value);
}

return properties;

I.e. create a dictionary of name and value pairs where name is the name of a property for objType and value is the value of the property for the instance objInstance of objType

Now converting this to an expression should produce a delegate that simply does

var properties = new Dictionary<string, object>();

properties.Add("Prop1", (object)objInstance.Prop1);
properties.Add("Prop2", (object)objInstance.Prop2);
properties.Add("Prop3", (object)objInstance.Prop3);

return properties;

I know how I can perform most of this, but what I am not sure on is how to create a local instance of a dictionary and then use it (and return) it subsequent expressions?





Aucun commentaire:

Enregistrer un commentaire