GetValue类上的,GetConstantValue和GetRawConstantValue方法有什么区别PropertyInfo?不幸的是,MSDN 文档在这个主题上不是很清楚。
4224 次
1 回答
24
两者GetConstantValue和GetRawConstantValue都旨在与文字一起使用(考虑const字段的情况,但从语义上讲,它不仅可以应用于字段) - 与GetValue在运行时获取某物的实际值不同,常量值(通过GetConstantValue或GetRawConstantValue)不是运行时依赖 - 它直接来自元数据。
GetConstantValue因此,我们得到和之间的区别GetRawConstantValue。基本上,后者是更直接和原始的形式。这主要是针对enum会员的;例如 - 如果我有一个:
enum Foo { A = 1, B = 2 }
...
const Foo SomeValue = Foo.B;
那么GetConstantValueofSomeValue是Foo.B; 但是,GetRawConstantValueofSomeValue是2. 特别是,GetConstantValue如果您使用的是仅反射上下文,则不能使用,因为这需要将值装箱为 a Foo,而在仅使用反射时不能这样做。
于 2013-08-07T09:13:18.800 回答