2

假设有一个枚举:

Public Enum MyEnum
    Value1 = 100
    Value2 = 200
    Value3 = 300
End Enum

如何实现扩展方法以获取此枚举的所有值的整数数组?

Dim ints As Integer() = GetType(MyEnum).ToIntArray()
' result: ints = {100, 200, 300}

(我已经看到扩展方法必须基于类型。)

4

1 回答 1

2
<System.Runtime.CompilerServices.Extension()> _
Public Function ToIntArray(Of T As Type)(a As T) As Integer()
    Return [Enum].GetValues(a).Cast(Of Integer)().ToArray
End Function
于 2013-07-26T07:28:23.250 回答