扩展方法来获取任何枚举的值

扩展方法来获取任何枚举的值

本文介绍了扩展方法来获取任何枚举的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试创建一个扩展方法,该方法适用于任何枚举,以返回其值。



而不是这样做:

  Enum.GetValues(typeof(BiasCode))。Cast< BiasCode>()

这样做很好:

  new BiasCode ).Values()

如果没有新的,甚至会更好,但是这是另一个问题。



我有一个中是不可能的。



但是,您可以在实践中获得足够的接近(并添加运行时检查以将其打开),以便您可以编写一个如下所示的方法:

  public static IEnumerable< TEnum>值< TEnum>()
其中TEnum:struct,IComparable,IFormattable,IConvertible
{
var enumType = typeof(TEnum);

//可选运行时检查完整性
if(!enumType.IsEnum)
{
throw new ArgumentException();
}

返回Enum.GetValues(enumType).Cast< TEnum>();
}

您可以使用

  var values = Values< BiasCode>();我已经使方法返回 IEnumerable< TEnum> 而不是额外的LINQ-y风格的列表,但是您可以平均返回一个真正的列表,其中包含 .ToList()。返回值


I've been trying to create an extension method, that would work on any enum, to return its values.

Instead of doing this:

Enum.GetValues(typeof(BiasCode)).Cast<BiasCode>()

It would be nice to do this:

new BiasCode().Values()

It would even be better without new, but that's another issue.

I have a .NET fiddle that has a solution that's close (code shown below). The problem with this code is that the extension method is returning List<int>. I would like to have it return a list of the enum values itself. Returning List<int> isn't terrible; it just means I have to cast the result.

Is it even possible to do this? I tried making the extension method generic, but ran into problems. This is as close as I was able to get:

using System;
using System.Linq;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        foreach (int biasCode in new BiasCode().Values())
        {
            DisplayEnum((BiasCode)biasCode);
        }
    }

    public static void DisplayEnum(BiasCode biasCode)
    {
        Console.WriteLine(biasCode);
    }
}

public enum BiasCode
{
    Unknown,
    OC,
    MPP
}

public static class EnumExtensions
{
    public static List<int> Values(this Enum theEnum)
    {
        var enumValues = new List<int>();
        foreach (int enumValue in Enum.GetValues(theEnum.GetType()))
        {
            enumValues.Add(enumValue);
        }

        return enumValues;
    }
}
解决方案

You can return an instance of the appropriate enum type (created using reflection), but its static type cannot be List<EnumType>. That would require EnumType to be a generic type parameter of the method, but then the type would have to be constrained to only enum types and that is not possible in C#.

However, you can get close enough in practice (and add runtime checks to top it off) so you can write a method that works like this:

public static IEnumerable<TEnum> Values<TEnum>()
where TEnum : struct,  IComparable, IFormattable, IConvertible
{
    var enumType = typeof(TEnum);

    // Optional runtime check for completeness
    if(!enumType.IsEnum)
    {
        throw new ArgumentException();
    }

    return Enum.GetValues(enumType).Cast<TEnum>();
}

which you can invoke with

var values = Values<BiasCode>();

I have made the method return IEnumerable<TEnum> instead of a list for the extra LINQ-y flavor, but you can trivially return a real list with .ToList() on the return value.

这篇关于扩展方法来获取任何枚举的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 06:59
查看更多