We have an enum in a class library:
Public Enum FieldType
Phone
Span
Gender
DrawPath
....
End Enum
which we use with an attribute applied to properties, for multiple scenarios: HTML/CSV rendering, onscreen display formatting among them.
Multiple projects use this class library, and we want to use project-specific values with this enum. The class library could have extension points (e.g. Public Shared FieldTypeAsHtml As Func(Of FieldType,String)
) which specific projects could override.
Each project could have its own additional enums or constants, and the conversions would be automatic (at least in VB.NET which is what we are using; C# would require explicit conversions).
Aside from the obvious -- the intention of this enum is to be "open" and not limited to a specified set of values, therefore no compile-time safety on a given value -- is this a valid design? Are there any downsides? Since there are no partial enums in .NET, what other alternatives might be better, and why?
-
This question was already discussed on Stackoverflow (more than once), see stackoverflow.com/questions/757684/enum-inheritanceDoc Brown– Doc Brown2015年11月12日 13:36:51 +00:00Commented Nov 12, 2015 at 13:36
-
@DocBrown The primary thrust of my question is on the advisability/inadvisability of doing this, which is not covered by the SO question.user20416– user204162015年11月12日 17:50:25 +00:00Commented Nov 12, 2015 at 17:50
-
Is it advisable to write programs in a typesafe manner? Short answer: it depends. Sometimes yes, sometimes no, and the reasoning about this will be pretty much the same as to your question.Doc Brown– Doc Brown2015年11月12日 18:57:07 +00:00Commented Nov 12, 2015 at 18:57
1 Answer 1
You could use the "Smart Enum" pattern. It is intended to be extended and typesafe. Although I have C# posts about it, but I think it would be helpful anyway. See:
-
Also: Smart Enums and some generic approach.Alexander Y.– Alexander Y.2015年11月20日 06:10:49 +00:00Commented Nov 20, 2015 at 6:10