C# get and set property by variable name

后端 未结 4 492
遇见更好的自我
遇见更好的自我 2021-01-01 23:46

Is there any way to do this? I try to test if a property of an object exists and if it does, I want to set a value to it. (Maybe the complete idea is bad, if true - why?)

4条回答
  •  有刺的猬
    2021-01-02 00:10

    var propertyInfo = info.GetType().GetProperty(propertyName);
    if (propertyInfo != null)  //this probably works. Yes it is
      {
            propertyInfo.SetValue(info, value, null);
      }
    

提交回复
热议问题