IEnumerable&Enum的扩展方法 [英] Extension Methods for IEnumerable<Enum>?

查看:147
本文介绍了IEnumerable&Enum的扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆不同的枚举,例如...

I have a bunch of different enums, such as...

 public enum MyEnum
{
  [Description("Army of One")]
  one, 
  [Description("Dynamic Duo")]
  two,
  [Description("Three Amigo's")]
  three,
  [Description("Fantastic Four")]
  four,
  [Description("The Jackson Five")]
  five
}

我为任何枚举写了一个扩展方法,以获取描述属性(如果有)。简单的...

I wrote an extension method for any Enum to get the Description attribute if it has one. Simple enough right...

public static string GetDescription(this Enum currentEnum)
{
  var fi = currentEnum.GetType().GetField(currentEnum.ToString());
  var da = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute));
  return da != null ? da.Description : currentEnum.ToString();
}

我可以非常简单地使用它,它的作用就像一个魅力,返回描述或ToString()。

I can use this very simply and it works like a charm, returning the description or ToString() as expected.

这是问题。我想有能力在IEnumerable的MyEnum,YourEnum或SomeoneElsesEnum上调用它。所以我就这样简单地写了以下扩展名。

Here is the problem though. I would like to have the ability to call this on an IEnumerable of MyEnum, YourEnum, or SomeoneElsesEnum. So I wrote the following extension just as simply.

public static IEnumerable<string> GetDescriptions(this IEnumerable<Enum> enumCollection)
{
  return enumCollection.ToList().ConvertAll(a => a.GetDescription());
}

这不行。它可以作为一种方法编译,但使用它会出现以下错误:

This doesn't work. It compiles fine as a method, but using it gives the following error:

Instance argument: cannot convert from 'System.Collections.Generic.IEnumerable<MyEnum>' to System.Collections.Generic.IEnumerable<System.Enum>'

那为什么会这样呢?
我可以使这项工作吗?

So why is this? Can I make this work?

我在此发现的唯一答案是编写泛型T的扩展方法如下:

The only answer I have found at this point is to write extension methods for generic T as follows:

public static IEnumerable<string> GetDescriptions<T>(this List<T> myEnumList) where T : struct, IConvertible
public static string GetDescription<T>(this T currentEnum) where T : struct, IConvertible

有人必须有一个更好的答案,或解释为什么我可以扩展枚举,但不是IEnumerable枚举...
任何人?

Someone must have a better answer for this, or an explanation of why I can extend an Enum but not an IEnumerable of Enum... Anyone?

推荐答案

.NET通用协方差仅适用于引用类型。这里, MyEnum 是一个值类型, System.Enum 是一个引用类型(从枚举类型转换为 System.Enum 是一个拳击操作)。

.NET generic covariance only works for reference types. Here, MyEnum is a value type, and System.Enum is a reference type (casting from an enum type to System.Enum is a boxing operation).

所以,一个 IEnumerable< MyEnum> / code>不是 IEnumerable< Enum> ,因为这会将每个枚举项目的值从值类型更改为引用类型;只允许使用表示保留转换。您需要使用您发布的通用方法技巧才能使其正常工作。

So, an IEnumerable<MyEnum> is not an IEnumerable<Enum>, as that would change the representation of each enumerated item from a value type to a reference type; only representation-preserving conversions are allowed. You need to use the generic method trick you've posted to get this to work.

这篇关于IEnumerable&Enum的扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆