string to variable name

You can use reflection to set the properties ‘by name’.

using System.Reflection;
...
myCustomer.GetType().GetProperty(item.Key).SetValue(myCustomer, item.Value, null);

You can also read the properties with GetValue, or get a list of all property names using GetType().GetProperties(), which returns an array of PropertyInfo (the Name property contains the properties name)

Leave a Comment