我想为给定的泛型Enum
类型生成基础值和名称的列表。我想要本机基础值(如object
s)。
public static ReadOnlyCollection<EnumerationMember> ListFromEnum<TEnum>()
{
Type enumType = typeof(TEnum);
if (!enumType.IsEnum)
{
throw new InvalidOperationException("TEnum is not an enum");
}
Type underlyingType = Enum.GetUnderlyingType(enumType);
TEnum[] enumValues = (TEnum[])Enum.GetValues(enumType);
return enumValues
.Select(ev => new EnumerationMember(
(???)ev,
EnumerationExtension.GetDescription(enumType, ev)))
.ToList()
.AsReadOnly();
}
public class EnumerationMember
{
public EnumerationMember(object value, string description)
{
this.Value = value;
this.Description = description;
}
public object Value { get; private set; }
public string Description { get; private set; }
}