【发布时间】:2014-02-27 07:56:38
【问题描述】:
与How to get the list of properties of a class? 密切相关,我已经了解了这个问题,但我很想知道哪些返回的属性是枚举。我的第一个(不太可能)猜测是这样的:
foo A;
foreach (var property in A.GetType().GetProperties())
{
if (property.PropertyType is Enum)
//Celebrate
}
这不起作用。它是有效的,但 Visual Studio 甚至能够提前警告“给定的表达式永远不是提供的 ('System.Enum') 类型”。
据我了解,C# 枚举是原始计数类型(默认为 int,但也可能是 byte、short 等)之上的包装器。我可以轻松地测试这些属性是否属于这些类型,但这会导致我在搜索 Enums 时出现很多误报。
【问题讨论】:
标签: c# .net reflection properties enums