[英]Foreach enum values without casting
I have to cast enum but I want this to be as generic as possbile. 我必须强制枚举,但我希望它像possbile一样通用。 How can I replace the Cast<XX>()
part with propertyType
? 我如何用propertyType
替换Cast<XX>()
部分?
foreach (var prop in MainType.GetProperties().Where(x => x.PropertyType.IsEnum))
{
var x = new { name = prop.Name, values = new List<object>() };
foreach (var v in Enum.GetValues(prop.PropertyType).Cast<XX>())
x.values.Add(new { p = v.GetAttribute<DescriptionAttribute>().Description,
c = v.GetAttribute<DefaultValueAttribute>().Value });
o.Add(x);
}
public static TAttribute GetAttribute<TAttribute>(this Enum value) where TAttribute : Attribute
{
var type = value.GetType();
var name = Enum.GetName(type, value);
return type.GetField(name)
.GetCustomAttributes(false)
.OfType<TAttribute>()
.SingleOrDefault();
}
public enum JobApplicationState : short
{
[Description("Active")]
[DefaultValue(typeof(string), "bg-primary text-highlight")]
Active = 1,
[Description("Passive")]
[DefaultValue(typeof(string), "bg-grey text-highlight")]
Passive = 2,
[Description("Rejected")]
[DefaultValue(typeof(string), "bg-danger text-highlight")]
Rejected = 3,
[Description("Accepted")]
[DefaultValue(typeof(string), "bg-success text-highlight")]
Accepted = 4
}
THIS WORKED! 这工作!
foreach (MemberInfo m in prop.PropertyType.GetFields())
{
if (m.GetCustomAttributes(typeof(DescriptionAttribute), true).FirstOrDefault() != null && m.GetCustomAttributes(typeof(DefaultValueAttribute), true).FirstOrDefault() != null)
{
x.values.Add(
new
{
p = ((DescriptionAttribute)m.GetCustomAttributes(typeof(DescriptionAttribute), true).FirstOrDefault()).Description,
c = ((DefaultValueAttribute)m.GetCustomAttributes(typeof(DefaultValueAttribute), true).FirstOrDefault()).Value
});
}
}
You should realize that an enum is nothing more than a named wrapper around an integral value. 您应该意识到,枚举只不过是围绕整数值的命名包装器。 I've described some details on how enums work here: Cast int to enum in C# . 我已经在这里描述了枚举的工作方式的一些细节: 在C#中将int转换为枚举 。
The question here is to get attributes values from an enum. 这里的问题是从枚举中获取属性值。 As you might imagine, this question is about getting attributes from the type , not about getting attributes from the value (there's no such thing). 您可能会想到,这个问题是关于从类型获取属性的,而不是关于从值获取属性的(没有这样的事情)。 Still, if you call a method like void Foo<T>(T myEnum)
, the type T will hold all the necessary info, even though 'in real life' the value of myEnum is passed around as an integral type. 尽管如此,如果您调用诸如void Foo<T>(T myEnum)
之类的方法,则类型T将保存所有必要的信息,即使“在现实生活中” myEnum的值也作为整数类型传递。
From this you can also deduce that you're actually looking for the attributes of the MemberInfo
's of T
, since you're looking for the members of the type (eg the enum). 由此,您还可以推断出您实际上是在寻找T
的MemberInfo
的属性,因为您正在寻找该类型的成员(例如,枚举)。 Again, all the details are in my post on how enums work. 同样,所有细节都在我的文章中有关枚举的工作方式。 Hence the answer: 因此,答案是:
foreach (MemberInfo m in prop.PropertyType.GetFields())
{
// use m.GetAttribute(...)
}
Why don´t you simply cast to Enum
: 您为什么不简单地投给Enum
:
foreach (var v in Enum.GetValues(prop.PropertyType).Cast<Enum>())
Now you can call your extension-method on every element within the returned list. 现在,您可以在返回列表中的每个元素上调用扩展方法。
v.GetAttribute<MyType>()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.