鉴于我的样本类:
public static class MyConstantClass
{
public static readonly Guid Prop1 = new Guid("d0f6f396-b75e-4312-bb54-09fbb40cf51e");
public static readonly Guid Prop2 = new Guid("398810e5-1ed2-468c-9f21-1af58dcecbbf");
}
到目前为止我尝试了什么:
var props= typeof(MyConstantClass).GetProperties(BindingFlags.Public |
BindingFlags.Static |
BindingFlags.FlattenHierarchy)
.ToList();
但是这会返回一个空列表。
答案 0 :(得分:6)
你的错误是这些Guids不是属性,而只是该类的字段:
var props = typeof(MyConstantClass).GetFields(BindingFlags.Public |
BindingFlags.Static |
BindingFlags.FlattenHierarchy)
.ToList();
使用GetProperties
定义您的类:
public static class MyConstantClass
{
public static Guid Prop1 { get; } = new Guid("d0f6f396-b75e-4312-bb54-09fbb40cf51e");
public static Guid Prop2 { get; } = new Guid("398810e5-1ed2-468c-9f21-1af58dcecbbf");
}