扩展和枚举类型

时间:2013-02-12 07:53:31

标签: c#-4.0 enums extension-methods

我正在尝试将Extension方法添加到Enum类型,但下面的代码失败。编译器在行StoreType.GetAllItems上给出错误如何将扩展方法添加到枚举类型?

namespace ConsoleApplication1
{

    public static class EnumExtensions
    {
        public static IEnumerable<T> GetAllItems<T>(this Enum value)
        {
            foreach (object item in Enum.GetValues(typeof(T)))
            {
                yield return (T)item;
            }
        }


    }

    class Program
    {

        [Flags]
        public enum StoreType
        {
            Paypal = 1,
            Plimus = 2,
            Other = 3
        };

        static void Main(string[] args)
        {            
           StoreType.GetAllItems //Fail here
        }
    }
}

3 个答案:

答案 0 :(得分:1)

您必须在值上调用GetAllItems,而不是类型:

StoreType.Paypal.GetAllItems()

但是不要把它变成一个扩展方法并将其声明为:

public static class EnumExtensions
{
    public static IEnumerable<T> GetAllItems<T>()
    {
        foreach (object item in Enum.GetValues(typeof(T)))
        {
            yield return (T)item;
        }
    }
}
static void Main(string[] args)
{            
    var allEnumItems = EnumExtensions.GetAllItems<StoreType>()
}

甚至:

Enum.GetValues(typeof (StoreType));
Enum.GetNames(typeof (StoreType));

之后您可以使用Enum.TryParse(...)将名称解析回枚举,我认为这就是您想要的?

答案 1 :(得分:1)

您看到编译器错误,因为无法覆盖默认的c#枚举,根据to this article尝试以这种方式覆盖您的枚举,方法是将您的方法添加到a自定义类:

public class MyEnum
{
    public static readonly MyEnum A = new MyEnum("A");
    public static readonly MyEnum B = new MyEnum("B");
    public static readonly MyEnum C = new MyEnum("C");
    public static IEnumerable<T> GetAllItems<T>(this Enum value)
    {
        foreach (object item in Enum.GetValues(typeof(T)))
        {
            yield return (T)item;
        }
    }

    public override string ToString()
    {
        return Value;
    }

    protected MyEnum(string value)
    {
        this.Value = value;
    }

    public string Value { get; private set; }
}

public sealed class MyDerivedEnum : MyEnum
{
    public static readonly MyDerivedEnum D = new MyDerivedEnum("D");

    private MyDerivedEnum(string value)
        : base(value)
    {
    }
}

class Program
{
    static void Main(string[] args)
    {
        MyEnum blah = MyEnum.A;
        System.Console.WriteLine(blah);
        blah = MyDerivedEnum.D;
        System.Console.WriteLine(blah);
    }
}

答案 2 :(得分:0)

我会说这是一个带有伪扩展名的类。可能看起来像这样:

用法:

/// <summary>Pseudo extension class for enumerations</summary>
/// <typeparam name="TEnum">Enumeration type</typeparam>
public class EnumUtils<TEnum> where TEnum : struct, IConvertible
{
    public static List<TEnum> ToList()
    {
        var enumType = typeof(TEnum);

        return enumType.IsEnum
            ? enumType.GetEnumValues().OfType<TEnum>().ToList()
            : throw new ArgumentException($"{enumType.Name} is not enum");
    }

    public static Dictionary<int, TEnum> ToDictionary()
    {
        Type enumType = typeof(TEnum);

        return enumType.IsEnum
            ? enumType.GetEnumValues().OfType<TEnum>().ToDictionary(
                e => Convert.ToInt32(Enum.Parse(typeof(TEnum), e.ToString()) as Enum), 
                e => e)
            : throw new ArgumentException($"{enumType.Name} is not enum");
    }
}

伪扩展类:

{{1}}

享受!