给出我的示例类:
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();
但是这返回一个空列表
你的错误是这些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");
}