Enumeration extension methods
Solution 1:
Yes, just code against the base Enum
type, e.g.
public static void Something(this Enum e)
{
// code here
}
The down-side is you'll probably end up doing some quite nasty stuff like finding the real base type using Enum.GetUnderlyingType
, casting, and going down different branches depending on what the base type of the enum is, but you can find some good uses for it (e.g. we have IsOneOf
and IsCombinationOf
methods that apply to all enums).
PS: Remember when writing the method that, although ill advised, you can use float
and double
as the base types for enums so you'll need some special cases for those as well as unsigned values.
Solution 2:
Yes, you can. The target extenstion type is of type Enum
. In C#, this would be done as:
public static void EnumExtension(this Enum e)
{
}
or like this in VB:
<Extension()> _
Public Sub EnumExtension(ByVal s As Enum)
End Sub