: , . Foo.GetEnumProperties "TestProp" :
public enum MyEnum
{
[XmlEnumAttribute("Twenty and Something")]
TwentyTree = 1,
[XmlEnumAttribute("Thirty and Something")]
Thirdyfour
}
class Foo
{
public MyEnum TestProp { get; set; }
public static string[] GetEnumProperties()
{
MemberInfo[] members = typeof(Foo).FindMembers(MemberTypes.Property, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance, null, null);
List<string> retList = new List<string>();
foreach (MemberInfo nextMember in members)
{
PropertyInfo nextProp = nextMember as PropertyInfo;
if (nextProp.PropertyType.IsEnum)
retList.Add(nextProp.Name);
} return retList.ToArray();
}
}
, , System.ComponentModel.DescriptionAttribute, :
public static string GetDescription(object value)
{
Type type = value.GetType();
string name = Enum.GetName(type, value);
if (name != null)
{
FieldInfo field = type.GetField(name);
if (field != null)
{
DescriptionAttribute attr = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
if (attr != null)
{
string desc = attr.Description;
return desc;
}
}
}
return value.ToString();
}