The following code was inspired and co-written by FourBlades.
The following method will take any combined enumerated value and return an array containing the constituent parts. It is intended to be used with enumerated types that have the Flags attribute applied, allowing you to combine multiple values into a single value, where each bit of the composite value represents one of the flags. An example of such a type is the FontStyle enumeration.
VB.NET Code:
Public Function GetConstituentFlags(Of T)(ByVal value As T) As T()
Dim genericType As Type = GetType(T)
If Not genericType.IsEnum Then
Throw New ArgumentException("Generic argument type must be an enumerated type.")
End If
Dim numericValue As Integer = Convert.ToInt32(value)
Dim flags As New List(Of T)
If numericValue = 0 Then
If [Enum].IsDefined(genericType, 0) Then
flags.Add(DirectCast([Enum].ToObject(genericType, 0), T))
End If
Else
For Each constant As Integer In [Enum].GetValues(genericType)
If constant <> 0 AndAlso (numericValue And constant) = constant Then
flags.Add(DirectCast([Enum].ToObject(genericType, constant), T))
End If
Next constant
End If
Return flags.ToArray()
End Function
This is a good example of a generic function. Many people will only be familiar with generics in relation to collections. If you want to see it in action, try editing the Bold, Italic, Underline and Strikethrough properties of a form's Font property and running the project with this code:
VB.NET Code:
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As EventArgs) Handles MyBase.Load
For Each style As FontStyle In Me.GetConstituentFlags(Me.Font.Style)
MessageBox.Show(style.ToString())
Next style
End Sub
Note that if you don't apply any styles to the font the method automatically returns the zero value for that enumeration, which is Regular. I haven't specifically tested it but this method should also support negative values.