我有一个通用方法,它使用反射来获取给定类型的PropertyInfo。
PropertyInfo[] properties = typeof(TItem).GetProperties();
如果其中一个属性恰好是 Enum(可以通过
property.PropertyType.IsEnum
轻松检查),我如何从 PropertyInfo 类中获取实际的 Enum 类?
我不确定我是否完全理解你的问题,但这通常是我使用的并且它适用于枚举。
System.Reflection.PropertyInfo[] properties = obj.GetType().GetProperties
(
System.Reflection.BindingFlags.Public
| System.Reflection.BindingFlags.NonPublic
| System.Reflection.BindingFlags.Instance
);
if (properties.Length > 0)
{
for (int i = 0; i < properties.Length; i++)
{
try
{
AppendToOutputCache
(
$"[{properties[i].Name}] : " +
$"{properties[i].PropertyType} : " +
$"{properties[i].GetValue(obj, default)}",
m_OutputCache
);
}
catch (Exception e)
{
AppendToOutputCache($"Exception:\n" + e.Message, m_OutputCache);
}
}
}