Enum.GetValues() Return Type

后端 未结 6 639
孤独总比滥情好
孤独总比滥情好 2020-12-01 11:45

I have read documentation that states that ‘given the type of the enum, the GetValues() method of System.Enum will return an array of the given enum\'s base type’ ie int, by

6条回答
  •  心在旅途
    2020-12-01 12:24

    You need to cast the result to the actual array type you want

    (Response[])Enum.GetValues(typeof(Response))
    

    as GetValues isn't strongly typed

    EDIT: just re-read the answer. You need to explicitly cast each enum value to the underlying type, as GetValues returns an array of the actual enum type rather than the base type. Enum.GetUnderlyingType could help with this.

提交回复
热议问题