Search code examples
c#.netlinqtypedescriptor

Any LINQ alternative for loop in reflection?


I have a type T with fields+properties that i'd like to populate from a dictionary, pref case insensitive

I can obviously do

foreach(PropertyInfo pi in t.GetProperties() )
.....

and then

foreach(FieldInfo pi in t.GetFields() )
.....

Hoping there's a better way, perhaps using LINQ ?


Solution

  • Here is an option,

    var propDict = t.GetProperties().ToLookup<PropertyInfo, string, Action<object, object>>(
        p => p.Name, p => (obj, val) => p.SetValue(obj, val, null));
    
    var fieldDict = t.GetFields().ToLookup<FieldInfo, string, Action<object, object>>(
        f => f.Name, f => (obj, val) => f.SetValue(obj, val));
    
    var memberDict = pDict.Concat(fDict).ToDictionary(p => p.Key, p => p.Value);
    

    ...and then to use something like...

    Dictionary<string, object> outDict;
    foreach (var keyval in outDict)
        memberDict[keyval.Key](container, keyval.Value);
    

    ...though not sure if it makes too much sense as going through properties/fields is relatively simple anyway, an idea maybe.