且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将数组转换为IEnumerable< T>

更新时间:2023-02-14 11:17:55

一般的 Array 基类不是类型,所以不实现任何类型特定的接口;然而,可以直接投射向量 - GetValues 实际返回一个向量;所以:

The general Array base class is not typed, so it does not implement any type-specific interfaces; however, a vector can be cast directly - and GetValues actually returns a vector; so:

public static IEnumerable<SomeType> AllEnums
    = (SomeType[])Enum.GetValues(typeof(SomeType));

或更简单:

public static SomeType[] AllEnums 
    = (SomeType[])Enum.GetValues(typeof(SomeType));