Is it possible to pass in a property name as a string and assign a value to it?

后端 未结 3 1866
终归单人心
终归单人心 2021-01-12 18:11

I\'m setting up a simple helper class to hold some data from a file I\'m parsing. The names of the properties match the names of values that I expect to find in the file.

3条回答
  •  天命终不由人
    2021-01-12 18:46

    Using reflection you get the property using the name and set its value... something like:

    Type t = this.GetType();
    var prop = t.GetProperty(propName);
    prop.SetValue(this, value, null);
    

提交回复
热议问题