I have created a method that inspects an object and returns a [requested] set of properties.
public static List<object> Inspect<T>(T obj, params Func<T, object>[] funcs)
{
List<object> results = new List<object>(funcs.Length);
foreach (var func in funcs)
{
results.Add(func(obj));
}
return results;
}
It is then invoked, for example on a List
, like so:
List<string> peopleData = new List<string>(10) { "name", "age", "address" };
List<object> properties = Inspect(peopleData, p => p.Count, p => p.Capacity);
// The results would be
// properties[0] = 3
// properties[1] = 10
I would like to adapt the Inspect
method to instead return a Dictionary<string, object>
, where the keys of the dictionary would be the property names. The adapted method would then be invoked like this:
List<string> peopleData = new List<string>(10) { "name", "age", "address" };
Dictionary<string, object> properties = Inspect(peopleData, p => p.Count, p => p.Capacity);
// The results would be
// properties["Count"] = 3
// properties["Capacity"] = 10
Is this possible? If so, and if the solution is reflection-based (as I assume it'd have to be), would there be a big performance hit?