Get value of c# dynamic property via string

前端 未结 11 1731
孤城傲影
孤城傲影 2020-11-27 11:12

I\'d like to access the value of a dynamic c# property with a string:

dynamic d = new { value1 = \"some\", value2 = \"random\", value3 = \"value\"

11条回答
  •  难免孤独
    2020-11-27 11:34

    Similar to the accepted answer, you can also try GetField instead of GetProperty.

    d.GetType().GetField("value2").GetValue(d);

    Depending on how the actual Type was implemented, this may work when GetProperty() doesn't and can even be faster.

提交回复
热议问题