I want to create an extension method that converts any enum into a Json string.
I'd like to be able to attach an extension method to Enum that I can pass an enum-type into, so that it can be called on Enum
in the way that you can call Enum.GetValues(Type enumType)
, so it might look like:
Enum.ToJsonString(Type enumType)
It seems that this is not possible -- Enum is an abstract class; how could you extend that? -- and that I'll have to create my own static type to implement this.
Can anyone either confirm that I cannot extend Enum this way (and please explain, technically, why), or tell me how I can?
Clarification (update)
I know I can do this with my own static type. For example, it would be fairly easy to create EnumExtensions.ToJsonString(Type enumType)
but I'd rather have the method listed alongside GetValues() et al with VS intellisence auto-complete.