简体   繁体   中英

VB.NET, INFER ON and Enum.GetValues

My project manager charged me to remove all "Option Infer On" directives from our solution (in order do not have undefined types, in his opinion).

Dim enums = [Enum].GetValues(enumType) ' here I will use "As Array" 

For Each item In enums ' <================== WHAT TYPE TO USE HERE?
  Dim ienum As [Enum] = CType(item, [Enum])

  Dim name As String
  Dim caption As String

  name = ienum.GetPropertyName
  caption = ienum.GetStringValue

  myDict.Add(caption, name)
Next item

The return type of GetValues is an Array of Objects. So the type of your "item" variable in the For Each loop will be Object.

Hope that helps.

According to MSDN it's an array of constants, so it will be an array of objects that you can cast to Integer.

FWIW I have got some code which does similar work, it picks up Attributes stored against each Enum value. For that I've used GetNames , then reflected back to the backing Enum field, to which I can search for Custom Attributes that store various bits of metadata such as "DisplayName" etc. Then the Enum can be reconstituted using [Enum].Parse() .

Hope that helps! Also, I think it's a dumb idea to have to change all your code because your manager doesn't trust the language/devs!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM