Refer to a property name by variable

前端 未结 4 677
不知归路
不知归路 2021-01-01 02:12

Is there a way to refer to a property name with a variable?

Scenario: Object A have public integer property X an Z, so...

public void setProperty(in         


        
4条回答
  •  长情又很酷
    2021-01-01 02:54

    I think you mean reflection:

    PropertyInfo info = myObject.GetType().GetProperty("NameOfProperty");
    info.SetValue(myObject, myValue);
    

提交回复
热议问题