我有一个看起来像这样的结构:
public struct MyStruct
{
public const string Property1 = "blah blah blah";
public const string Property2 = "foo";
public const string Property3 = "bar";
}
Run Code Online (Sandbox Code Playgroud)
我想以编程方式检索MyStruct的const属性值的集合.到目前为止,我已经尝试过这个并没有成功:
var x = from d in typeof(MyStruct).GetProperties()
select d.GetConstantValue();
Run Code Online (Sandbox Code Playgroud)
有人有主意吗?谢谢.
编辑:这是最终为我工作的:
from d in typeof(MyStruct).GetFields()
select d.GetValue(new MyStruct());
Run Code Online (Sandbox Code Playgroud)
感谢Jonathan Henson和JaredPar的所有帮助!
Jar*_*Par 16
这些字段不是属性,因此您需要使用该GetFields方法
var x = from d in typeof(MyStruct).GetFields()
select d.GetRawConstantValue();
Run Code Online (Sandbox Code Playgroud)
另外我相信你正在寻找的方法GetRawConstantValue而不是GetConstantValue