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 compile to a delegate that simply does
Func<T, Dictionary<string, object>> func = i =>
{
var properties = new Dictionary<string, object>();
properties.Add("Prop1", (object)i.Prop1);
properties.Add("Prop2", (object)i.Prop2);
properties.Add("Prop3", (object)i.Prop3);
// depending upon the number of properties of T, Add will continue
return properties;
};
I know how to perform some 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) in subsequent expressions?
Func<object, Dictionary<string, object>>
orExpression<Func<object, Dictionary<string, object>>>
? – Counterman