在java中,我可以轻松地描述带有aditional数据的枚举.
我可以形容这样的事情
public enum OperatorType
{
GreaterOrEqual (">=", "GreaterOrEqual"),
Greater (">" ,"Greater"),
Less ("<", "Less"),
LessOrEqual ("<=", "LessOrEqual"),
Equal ("==", "Equal"),
Between ("Between", "Between"),
Around ("Around","Around");
private final String symbol;
private final String name;
private OperatorType(final String symbol, final String name) {
this.symbol = symbol;
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
然后添加一个迭代values()的静态方法,将所有数据添加到hashmap中,并允许通过其中一个属性从映射中检索完整枚举数据作为键.
简而言之,enum是java中非常发达的类型.
现在,
转到c#,我的选择是什么?
我想要保存带有属性的枚举,将其加载到地图中,并在需要时按键进行检索.我有什么需要帮助的吗(比如每个枚举的单音 - 这不是一个好主意).
谢谢.
我只想创建一个类public static readonly
,每个类型的实例和沟渠枚举.您可以将它们用作字典键或做任何您喜欢的事情.如果您仍打算将它们映射到基础数据类型(int
),那么您也可以为它创建隐式运算符.
public class OperatorType
{
private static readonly Dictionary<int, OperatorType> OperatorMapping = new Dictionary<int, OperatorType>();
public static readonly OperatorType GreaterOrEqual = new OperatorType(0, ">=", "GreaterOrEqual");
public static readonly OperatorType Greater = new OperatorType(1, ">", "Greater");
public readonly String symbol;
public readonly String name;
private readonly int underlyingValue;
private OperatorType(int underlyingValue, string symbol, string name) {
this.underlyingValue = underlyingValue;
OperatorMapping[underlyingValue] = this;
this.symbol = symbol;
this.name = name;
}
public static implicit operator int(OperatorType operatorType)
{
return operatorType.underlyingValue;
}
public static implicit operator OperatorType(int value)
{
return OperatorMapping[value];
}
}
Run Code Online (Sandbox Code Playgroud)
样品用法:
Dictionary<OperatorType, string> operators = new Dictionary<OperatorType, string>();
operators.Add(OperatorType.GreaterOrEqual, "Greater or equal");
Console.WriteLine(operators[OperatorType.GreaterOrEqual]); //"Greater or equal"
OperatorType operatorType = 1;
Console.WriteLine(operatorType.name); //"Greater"
Run Code Online (Sandbox Code Playgroud)
如果您不关心基础值,请不要包含它.还要考虑Dictionary
映射是否应该是线程安全的.您还可以公开静态IEnumerable<OperatorType>
(或其他集合)以根据需要定义所有运算符.
编辑:第二个想法,explicit
运营商可能更可取代implicit
,以符合典型的.NET最佳实践,并更好地匹配典型的enum
转换.