C# GetValue、GetConstantValue和GetRawConstantValue之间的差异

C# GetValue、GetConstantValue和GetRawConstantValue之间的差异,c#,reflection,C#,Reflection,PropertyInfo类上的GetValue、GetConstantValue和GetRawConstantValue方法之间有什么区别?不幸的是,MSDN文档在这个问题上不是很清楚。这两个GetConstantValue和GetRawConstantValue都是用于文本的(在字段的情况下考虑const,但从语义上来说,它可以应用于不止是字段)-与运行时获取某物实际值的GetValue不同,常量值(通过GetConstantValue或GetRawConstantValue)与运行时无关-它

PropertyInfo
类上的
GetValue
GetConstantValue
GetRawConstantValue
方法之间有什么区别?不幸的是,MSDN文档在这个问题上不是很清楚。

这两个
GetConstantValue
GetRawConstantValue
都是用于文本的(在字段的情况下考虑
const
,但从语义上来说,它可以应用于不止是字段)-与运行时获取某物实际值的
GetValue
不同,常量值(通过
GetConstantValue
GetRawConstantValue
)与运行时无关-它直接来自元数据

然后我们得到
GetConstantValue
GetRawConstantValue
之间的区别。基本上,后者是更直接和原始的形式。这主要为
enum
成员显示;例如,如果我有一个:

enum Foo { A = 1, B = 2 }
...
const Foo SomeValue = Foo.B;

然后
SomeValue
GetConstantValue
Foo.B
;但是,
SomeValue
GetRawConstantValue
2
。特别是,如果您使用的是仅反射上下文,则不能使用
GetConstantValue
,因为这需要将值装箱到
Foo
,而仅使用反射时不能这样做。

Marc您的解释涵盖了
GetRawConstantValue
关于
属性的字段。你不可能是对的吗?如果我弄错了,你可以用
属性
@SriramSakthivel展示一个例子吗?好吧,这里有一个问题,因为AFAIK C#不包括一个用常量语义表示属性的机制。IL包含的内容比任何一种语言都多。所以没有:我不相信有可能给出一个例子,除了编写原始IL。