我持有枚举类型的结构化只读数据,现在我想扩展结构并为枚举中的每个值添加额外的字段。所以,我原来的枚举是:
public enum OutputFormats { Pdf, Jpg, Png, Tiff, Ps };
我想像这样扩展它们:
Value=Pdf
FileName="*.PDF"
ID=1
Value=Jpg
FileName="*.jpg"
ID=2
...等等。
枚举不能保存多维数据结构,那么通常认为保存此类结构化数据的最佳“位置”是什么?我是否应该创建一个具有 value
、filename
和 id
属性的类并在类构造函数中初始化数据?
最佳答案
也许这个伪枚举模式会有用:
public class OutputFormats
{
public readonly string Value;
public readonly string Filename;
public readonly int ID;
private OutputFormats(string value, string filename, int id)
{
this.Value = value;
this.Filename = filename;
this.ID = id;
}
public static readonly OutputFormats Pdf = new OutputFormats("Pdf", "*.PDF", 1);
public static readonly OutputFormats Jpg = new OutputFormats("Jpg", "*.JPG", 2);
}
另一种变体,也许更简洁:
public class OutputFormats
{
public string Value { get; private set; }
public string Filename { get; private set; }
public int ID { get; private set; }
private OutputFormats() { }
public static readonly OutputFormats Pdf = new OutputFormats() { Value = "Pdf", Filename = "*.PDF", ID = 1 };
public static readonly OutputFormats Jpg = new OutputFormats() { Value = "Jpg", Filename = "*.JPG", ID = 2 };
}
关于c# - 保存只读结构化数据的最佳 "place"是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6579331/