我有一个商业子码提供的枚举。不幸的是,我意识到枚举值的描述在用户之间是不同的。
这是我在一个完美的世界中试图实现的目标,但我知道在运行时不能更改属性值。
public enum BusinessSubcodesEnum
{
[Representation(@"Cannot charge customers basket. No default currency set for user: {0}.")]
DefaultCurrencyNotSet = 18
}值得注意的是,上面有上百个这样的子代码,所以我真的很想避免使用BusinessCode和Description字段的上百个类。我怎样才能克服这一切?摘要/通用类?
发布于 2018-04-17 15:23:02
听起来现在是使用“强类型枚举”的好时机。这是一种类似于常规枚举的结构,但它可以缓解枚举所具有的行为缺陷。
创建一个类BusinessSubCode并在该类中创建相同类型的静态属性。现在,您可以使用相同的行为,即BusinessSubCode.DefaultCurrencyNotSet,并访问这些静态属性的属性。
public class BusinessSubCode
{
public static BusinessSubCode DefaultCurrencyNotSet { Representation = "Enter text here", Value = 18 };
public static BusinessSubCode DefaultCurrencySet { Representation = "Enter other text here", Value = 28 };
public string Representation {get;}
public int Value {get;}
}您还可以重写ToString()方法,并使用其他技巧使其行为像枚举,但具有扩展的功能。查看这个伟大的博客文章获得更多和更好的源代码。
您可以像往常一样向类添加任何属性和方法。
https://stackoverflow.com/questions/49881350
复制相似问题