GetValue,GetConstantValue和GetRawConstantValue之间的区别

Lev*_*lho 13 c# reflection

是什么之间的差异GetValue,GetConstantValue以及GetRawConstantValue对方法PropertyInfo的类?遗憾的是,MSDN文档在这个问题上并不十分清楚.

Mar*_*ell 16

双方GetConstantValueGetRawConstantValue打算为使用文字(认为const在场的情况下,但在语义上它可以应用到更多的不仅仅是场) -不像GetValue它会在运行时得到的东西的实际价值,一个恒定值(通过GetConstantValueGetRawConstantValue)不运行时依赖 - 它直接来自元数据.

那么我们得到GetConstantValue和之间的区别GetRawConstantValue.基本上,后者是更直接和原始的形式.这主要表示enum会员; 例如 - 如果我有:

enum Foo { A = 1, B = 2 }
...
const Foo SomeValue = Foo.B;
Run Code Online (Sandbox Code Playgroud)

GetConstantValueSomeValueIS Foo.B; 然而,GetRawConstantValueSomeValue2.特别是,GetConstantValue如果您使用的是仅反射上下文,则无法使用,因为这需要将值装入 a Foo,这在使用仅反射时无法执行.