更新时间:2023-12-01 11:44:10
C#枚举不喜欢这个工作。然而,你可以很轻松地实现自己的固定的值:
C# enums don't work well like this. However, you can implement your own "fixed set of values" fairly easily:
public sealed class Foo
{
public static readonly Foo FirstValue = new Foo(...);
public static readonly Foo SecondValue = new Foo(...);
private Foo(...)
{
}
// Add methods here
}
碰巧的是,其中一个例子,我得到了,这是非常相似你的 - 的 DateTimeFieldType
在野田佳彦时间。有时你甚至可能要进行启封类,但保留私有构造 - 它允许你创建子类的只为嵌套类的。 。非常方便的限制继承
As it happens, one example I've got of this is remarkably similar to yours - DateTimeFieldType
in Noda Time. Sometimes you might even want to make the class unsealed, but keep the private constructor - which allows you to create subclasses only as nested classes. Very handy for restricting inheritance.
缺点是,你不能使用开关:(
The downside is that you can't use switch :(