如何从扩展枚举类中获取所有值? [英] How do I get all the values from my extended enum class?

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

问题描述

我决定在我的项目中的其中一个 enums 可能会从更加智能化中获益,并且在下一页上遵循Microsoft示例:



Microsoft Extended Enum



这是我的代码的简化版本:



 public abstract class Enumeration:IComparable 
{
protected Enumeration(){}

protected Enumeration(int id,string name)
{
Id = id;
姓名=姓名;
}

public int Id {get; }

public string Name {get; }

public int CompareTo(object other)=> Id.CompareTo(((Enumeration)other).Id);

公共覆盖字符串ToString()=>名称;

public static IEnumerable< T> GetAll< T>()其中T:Enumeration,new()
{
Type type = typeof(T);
FieldInfo [] fields = type.GetTypeInfo()。GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
foreach(字段中的var info)
{
var instance = new T();
var locatedValue = info.GetValue(instance)as T;
if(locatedValue!= null)
yield return locatedValue;
}
}

公共覆盖bool Equals(object obj)
{
if(!(obj is Enumeration otherValue))return false;
var typeMatches = GetType()。Equals(obj.GetType());
var valueMatches = Id.Equals(otherValue.Id);
返回typeMatches&& valueMatches;
}

public override int GetHashCode()
{
return Id.GetHashCode();
}
}


公共类水果:枚举
{
公共静态水果Apple =新AppleFruitType();
public static Fruit Banana = new BananaFruitType();
public static Fruit Orange = new OrangeFruitType();

protected Fruit(int id,string name):base(id,name){}

private class AppleFruitType:Fruit
{
public AppleFruitType ():base(0,Apple){}
}

私有类BananaFruitType:Fruit
{
public BananaFruitType():base(1, Banana){}
}

私有类OrangeFruitType:Fruit
{
public OrangeFruitType():base(2,Orange){}
}
}


公共类MainApplication
{
public void SomeCode()
{
//这些都不是三行将编译。
列表< Fruit> AllFruits1 = Fruit.GetAll();
列表< Fruit> AllFruits2 = Fruit.GetAll< Fruit>();
列表< Fruit> AllFruits3 = Fruit.GetAll< Enumeration>();
列表< Fruit> AllFruits4 = Fruit.GetAll()。ToList< Fruit> ;;
列表< Fruit> AllFruits5 = Fruit.GetAll< Fruit>()。ToList< Fruit> ;;
列表< Fruit> AllFruits6 = Fruit.GetAll< Enumeration>()。ToList< Fruit> ;;
}
}





虽然关于为什么这六行不起作用的编译器信息似乎很清楚,我无法弄清楚我需要写什么才能让这个工作。



任何人都可以用我的语言知识解释这个明显的明显漏洞吗?



我尝试过:



上面代码中显示的示例。

解决方案

Fruit必须有一个无参数构造函数。

添加

  public  Fruit()
{
}



到您的Fruit类和

  var  AllFruits2 = Fruit.GetAll< Fruit>(); 



应该有效。


获取特定类型,例如水果,你可以这样做:

 IEnumerable< Fruit> AllFruits1 = Enumeration.GetAll< Fruit>(); 





如果你有另一种类似CardType的扩展枚举类型:



  public   abstract   class  CardType:枚举
...


IEnumerable< CardType> AllCardType1 = Enumeration.GetAll< CardType>();
IEnumerable< Fruit> AllFruits1 = Enumeration.GetAll< Fruit>();





基本上,Enumeration类的目的是让你轻松获得所有枚举类型


I decided one of the enums in my project might benefit from being more intelligent and followed the Microsoft example on the following page:

Microsoft Extended Enum

This is a cut-down version of my code:

public abstract class Enumeration : IComparable
{
    protected Enumeration() {}

    protected Enumeration( int id, string name )
    {
        Id = id;
        Name = name;
    }

    public int Id { get; }

    public string Name { get; }

    public int CompareTo( object other ) => Id.CompareTo( ((Enumeration)other).Id );

    public override string ToString() => Name;

    public static IEnumerable<T> GetAll<T>() where T : Enumeration, new()
    {
        Type type = typeof( T );
        FieldInfo[] fields = type.GetTypeInfo().GetFields( BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly );
        foreach( var info in fields )
        {
            var instance = new T();
            var locatedValue = info.GetValue( instance ) as T;
            if( locatedValue != null )
                yield return locatedValue;
        }
    }
    
    public override bool Equals(object obj)
    {
        if( !( obj is Enumeration otherValue ) ) return false;
        var typeMatches = GetType().Equals(obj.GetType());
        var valueMatches = Id.Equals(otherValue.Id);
        return typeMatches && valueMatches;
    }

    public override int GetHashCode()
    {
        return Id.GetHashCode();
    }
}


public class Fruit : Enumeration
{
    public static Fruit Apple = new AppleFruitType();
    public static Fruit Banana = new BananaFruitType();
    public static Fruit Orange = new OrangeFruitType();

    protected Fruit( int id, string name ) : base( id, name ) { }

    private class AppleFruitType : Fruit
    {
        public AppleFruitType() : base( 0, "Apple" ) {}
    }

    private class BananaFruitType : Fruit
    {
        public BananaFruitType() : base( 1, "Banana" ) {}
    }

    private class OrangeFruitType : Fruit
    {
        public OrangeFruitType() : base( 2, "Orange" ) {}
    }
}


public class MainApplication
{
    public void SomeCode()
    {
        // None of these three lines will compile.
        List<Fruit> AllFruits1 = Fruit.GetAll();
        List<Fruit> AllFruits2 = Fruit.GetAll<Fruit>();
        List<Fruit> AllFruits3 = Fruit.GetAll<Enumeration>();
    	List<Fruit> AllFruits4 = Fruit.GetAll().ToList<Fruit>;
    	List<Fruit> AllFruits5 = Fruit.GetAll<Fruit>().ToList<Fruit>;
    	List<Fruit> AllFruits6 = Fruit.GetAll<Enumeration>().ToList<Fruit>;
    }
}



Although the compiler information on why these six lines don't work seems to be clear, I can't figure out what I need to write to get this working.

Can anyone please explain this obvious glaring hole in my language knowledge?

What I have tried:

The examples shown in the above code.

解决方案

Fruit must have a parameterless constructor.
Add

public Fruit()
{
}


to your Fruit class and

var AllFruits2 = Fruit.GetAll<Fruit>();


should work.


To get a specific type e.g. Fruit, you can do this:

IEnumerable<Fruit> AllFruits1 = Enumeration.GetAll<Fruit>();



If you have another extended enum type like CardType:

public abstract class CardType : Enumeration
...


IEnumerable<CardType> AllCardType1 = Enumeration.GetAll<CardType>();
IEnumerable<Fruit> AllFruits1 = Enumeration.GetAll<Fruit>();



Basically the purpose of Enumeration class is that you can easily get your all enum types


这篇关于如何从扩展枚举类中获取所有值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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