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。