There's no way to use extension methods to do this to a specific enum. You either need to extend your extension method to support all Enum
types, add an is
statement in there which you can use to only cast it when necessary, or write a special overload to this function which you call just for this type of enum. This has to do with how extension methods are actually implemented.
The compiler turns an extension method into its static form: myCopyType.ToString()
becomes CopyType.ToString(myCopyType)
when compiled. But with your scenario (or even with generics) the compiler can't tell what type to use, because the type isn't determined until runtime.
This leaves the three choices above.
In my own code, I went with the first option, based on the code here. You'll be able to call .GetLabel()
on any Enum
type, and you can put your special labels on this one specifically.
Additionally, you'll need to use foreach (Enum val in ...
instead, so as to make sure the compiler knows it's an Enum
.