Scenario: I have this class
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
User has the ability to only select for example FirstName
and Age
.
Currently I am using reflection but I have been told that reflection hurts performance among other things.
public static class Helper<T>GetObject(List<string> fieldsToSet, Dictionary<string, string> values)
{
var returnObject = new T();
foreach (string field in fieldsToSet)
{
var property = returnObject.GetType().GetProperty(field);
property.SetValue(returnObject, values[field], null);
}
return returnObject;
}
Can anyone show me an example of how to do this (setting only a subset of all the fields in a class) using lightweight code generation?