Get and set the field value by passing name

后端 未结 3 795
再見小時候
再見小時候 2020-12-10 20:49

I have a field in a class with a random name like:

class Foo {
    public string a2de = \"e2\"
}

I have the name of this field in another v

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-10 21:28

    You can potentially do it with reflection (e.g. Type.GetField etc) - but that should generally be a last resort.

    Have you considered using a Dictionary and using the "variable name" as the key instead?

提交回复
热议问题