Property Name and need its value

后端 未结 5 979
萌比男神i
萌比男神i 2020-12-17 06:42

I have a name of a property and need to find its value within a Class, what is the fastest way of getting to this value?

5条回答
  •  眼角桃花
    2020-12-17 07:46

    At runtime you can use reflection to get the value of the property.

    Two caveats:

    • Obfuscation: An obfuscator may change the name of the property, which will break this functionality.

    • Refactoring: Using reflection in this manner makes the code more difficult to refactor. If you change the name of the property, you may have to search for instances where you use reflection to get the property value based upon name.

提交回复
热议问题