Not sure if this is possible, but here is what I am trying to do:
I want to have a dictionary that contains a mapping of a column index to a property name used to populate that index.
In my code I will loop through an array if strings and use the dictionary to look up which column it should map to.
My end result code would look like:
for(int index = 0; index < fields.Length)
{
fieldPropertyMapping[index] = StripQuotes(fields[index]);
}
To do what you’re asking specifically, you’ll have to use reflection (as you tagged your question) to do this. Have a look at the
PropertyInfoclass. I’m not entirely certain what your code is doing, but a general example of reflectively setting a property value would be:You could, however, pass an
Action<T>instead, if you know the property at some point in the code. For example:Or, if you know the type when you call it but you don’t have the instance, you could make it an
Action<YourType, PropertyType>. This also would prevent creating a closure.To make this fully generic (“generic” as in “non-specific”, not as in generics), you’ll probably have to make it an
Action<object>and cast it to the proper property type within the lambda, but this should work either way.