What is the difference between the GetValue
, GetConstantValue
and GetRawConstantValue
methods on the PropertyInfo
class? The MSDN documentation unfortunately isn't very clear on the subject.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
I don't know what you are trying to do. Anyway, if you simply want to retrieve the value of a property using reflection you have to use GetValue. I.e. something like this:
Note that if you call GetConstantValue or GetRawConstantValue in this example you get an InvalidOperationexception, since the property is not constant.
The difference between GetConstantValue and GetRawConstantValue has been explained perfectly by Marc.
Both
GetConstantValue
andGetRawConstantValue
are intended for use with literals (thinkconst
in the case of fields, but semantically it can apply to more than just fields) - unlikeGetValue
which would get the actual value of something at runtime, a constant value (viaGetConstantValue
orGetRawConstantValue
) is not runtime dependent - it is direct from metadata.So then we get to the difference between
GetConstantValue
andGetRawConstantValue
. Basically, the latter is the more direct and primitive form. This shows mainly forenum
members; for example - if I had an:then the
GetConstantValue
ofSomeValue
isFoo.B
; however, theGetRawConstantValue
ofSomeValue
is2
. In particular, you can't useGetConstantValue
if you are using a reflection-only context, as that would require boxing the value to aFoo
, which you can't do when using reflection-only.