c# - 保存只读结构化数据的最佳 "place"是什么?

标签 c# class struct

我持有枚举类型的结构化只读数据,现在我想扩展结构并为枚举中的每个值添加额外的字段。所以,我原来的枚举是:

public enum OutputFormats { Pdf, Jpg, Png, Tiff, Ps };

我想像这样扩展它们:

Value=Pdf
FileName="*.PDF"
ID=1

Value=Jpg
FileName="*.jpg"
ID=2

...等等。

枚举不能保存多维数据结构,那么通常认为保存此类结构化数据的最佳“位置”是什么?我是否应该创建一个具有 valuefilenameid 属性的类并在类构造函数中初始化数据?

最佳答案

也许这个伪枚举模式会有用:

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/

相关文章:

c - 如何在另一个结构中定义结构变量?

c++ - 如何解决嵌入结构中的运算符重载的歧义?

c# - 在带有子窗体的 WinForms 应用程序中进行依赖注入(inject)

c# - WinAPI MoveWindow 功能不适用于某些窗口

c++ - 作为参数传递的类对象,访问自己的私有(private)成员

c# 嵌套类找不到类型或命名空间

dictionary - Go 中复杂键字典的唯一性,但 Julia 中没有?

c# - 在没有 .select 的情况下拆分字符串并删除空格

c# - 将参数从一种方法传递到 button.click 的另一种方法

python - Python中从一个类中调用多个函数而无需每次都重复类名