问题描述
是什么的GetValue
, GetConstantValue
和 GetRawConstantValue $ C之间的差异$ C上的
的PropertyInfo
类>方法呢? MSDN文档不幸的是,没有关于这个问题很清楚。
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.
推荐答案
两个 GetConstantValue
和 GetRawConstantValue
拟用于场的情况下,用文字使用(想想常量
,但语义的它可以应用到的不仅仅是场) - 不像的GetValue
这将通过<$ C $得到的东西在运行时,一个恒定的值(实际值C> GetConstantValue 或 GetRawConstantValue
)不运行时依赖 - 它是由元数据直接
Both GetConstantValue
and GetRawConstantValue
are intended for use with literals (think const
in the case of fields, but semantically it can apply to more than just fields) - unlike GetValue
which would get the actual value of something at runtime, a constant value (via GetConstantValue
or GetRawConstantValue
) is not runtime dependent - it is direct from metadata.
于是,我们得到 GetConstantValue
和 GetRawConstantValue
之间的差异。基本上,后者是更直接,更原始的形式。这主要显示了枚举
成员;例如 - 如果我有一个:
So then we get to the difference between GetConstantValue
and GetRawConstantValue
. Basically, the latter is the more direct and primitive form. This shows mainly for enum
members; for example - if I had an:
enum Foo { A = 1, B = 2 }
...
const Foo SomeValue = Foo.B;
那么 GetConstantValue
<$ C $的C> someValue中是 Foo.B
;但是, GetRawConstantValue
someValue中
是 2
。特别是,你不能使用 GetConstantValue
如果您使用的是只反射上下文中,因为这将需要的拳击的值到富
,它使用反射只有当你不能做。
then the GetConstantValue
of SomeValue
is Foo.B
; however, the GetRawConstantValue
of SomeValue
is 2
. In particular, you can't use GetConstantValue
if you are using a reflection-only context, as that would require boxing the value to a Foo
, which you can't do when using reflection-only.
这篇关于的GetValue,GetConstantValue和GetRawConstantValue的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!