Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 替换需要转换和枚举的枚举_C#_Enums - Fatal编程技术网

C# 替换需要转换和枚举的枚举

C# 替换需要转换和枚举的枚举,c#,enums,C#,Enums,我们有一些东西可以输出成各种格式。目前,我们使用以下枚举表示这些格式: [Flags] public enum ExportFormat { None = 0x0, Csv = 0x1, Tsv = 0x2, Excel = 0x4, All = Excel | Csv | Tsv } private static Dictionary<ExportFormat, string> FormatDescriptions = new Di

我们有一些东西可以输出成各种格式。目前,我们使用以下枚举表示这些格式:

[Flags]
public enum ExportFormat
{
    None = 0x0,
    Csv = 0x1,
    Tsv = 0x2,
    Excel = 0x4,
    All = Excel | Csv | Tsv
}
private static Dictionary<ExportFormat, string> FormatDescriptions =
    new Dictionary<ExportFormat,string>()
{
    { ExportFormat.Csv, "Comma Separated Values" },
    { ExportFormat.Tsv, "Tab Separated Values" },
    { ExportFormat.Excel, "Microsoft Excel 2007" },            
};

public static string Describe(this ExportFormat e)
{
    var formats = e.Formats();
    var descriptions = formats.Select(fmt => FormatDescriptions[fmt]);

    return string.Join(", ", descriptions.ToArray());
}
问题是,必须枚举这些内容,并且它们还需要在ui中进行翻译或描述。目前我通过创建两个扩展方法解决了这个问题。他们工作,但我真的不喜欢他们或解决方案在所有。。。他们觉得有点臭。问题是我真的不知道怎样才能做得更好。有人有好的选择吗?以下是两种方法:

    public static IEnumerable<ExportFormat> Formats(this ExportFormat exportFormats)
    {
        foreach (ExportFormat e in Enum.GetValues(typeof (ExportFormat)))
        {
            if (e == ExportFormat.None || e == ExportFormat.All)
                continue;

            if ((exportFormats & e) == e)
                yield return e;
        }
    }

    public static string Describe(this ExportFormat e)
    {
        var r = new List<string>();

        if ((e & ExportFormat.Csv) == ExportFormat.Csv)
            r.Add("Comma Separated Values");

        if ((e & ExportFormat.Tsv) == ExportFormat.Tsv)
            r.Add("Tab Separated Values");

        if ((e & ExportFormat.Excel) == ExportFormat.Excel)
            r.Add("Microsoft Excel 2007");

        return r.Join(", ");
    }
公共静态IEnumerable格式(此ExportFormat exportFormats)
{
foreach(Enum.GetValues中的ExportFormat e(typeof(ExportFormat)))
{
if(e==ExportFormat.None | | e==ExportFormat.All)
继续;
if((exportFormats&e)==e)
收益率e;
}
}
公共静态字符串描述(此导出格式为e)
{
var r=新列表();
if((e&ExportFormat.Csv)==ExportFormat.Csv)
r、 添加(“逗号分隔值”);
if((e&ExportFormat.Tsv)==ExportFormat.Tsv)
r、 添加(“制表符分隔值”);
if((e&ExportFormat.Excel)==ExportFormat.Excel)
r、 添加(“Microsoft Excel 2007”);
返回r.Join(“,”);
}

也许这就是解决这个问题的方法,但我觉得一定有更好的方法。我如何重构它?

我想到的唯一其他方法是使用System.Attribute类

public class FormatDescription : Attribute
{
    public string Description { get; private set; }

    public FormatDescription(string description)
    {
        Description = description;
    }
}
然后在描述函数中使用反射。
这种方法的唯一好处是将定义和描述放在一个地方

我想到的唯一其他方法是使用System.Attribute类

public class FormatDescription : Attribute
{
    public string Description { get; private set; }

    public FormatDescription(string description)
    {
        Description = description;
    }
}
然后在描述函数中使用反射。
这种方法的唯一好处是将定义和描述放在一个地方

您可以使用描述中的Formats方法来避免在多个位置执行所有位操作,如下所示:

[Flags]
public enum ExportFormat
{
    None = 0x0,
    Csv = 0x1,
    Tsv = 0x2,
    Excel = 0x4,
    All = Excel | Csv | Tsv
}
private static Dictionary<ExportFormat, string> FormatDescriptions =
    new Dictionary<ExportFormat,string>()
{
    { ExportFormat.Csv, "Comma Separated Values" },
    { ExportFormat.Tsv, "Tab Separated Values" },
    { ExportFormat.Excel, "Microsoft Excel 2007" },            
};

public static string Describe(this ExportFormat e)
{
    var formats = e.Formats();
    var descriptions = formats.Select(fmt => FormatDescriptions[fmt]);

    return string.Join(", ", descriptions.ToArray());
}
专用静态字典格式说明=
新字典()
{
{ExportFormat.Csv,“逗号分隔值”},
{ExportFormat.Tsv,“制表符分隔值”},
{ExportFormat.Excel,“MicrosoftExcel2007”},
};
公共静态字符串描述(此导出格式为e)
{
var formats=e.formats();
变量描述=格式。选择(fmt=>FormatDescriptions[fmt]);
返回字符串.Join(“,”,descriptions.ToArray());
}

通过这种方式,很容易合并来自外部源或本地化的字符串描述,如上所述。

您可以使用描述中的Formats方法来避免在多个位置执行所有位操作,例如:

[Flags]
public enum ExportFormat
{
    None = 0x0,
    Csv = 0x1,
    Tsv = 0x2,
    Excel = 0x4,
    All = Excel | Csv | Tsv
}
private static Dictionary<ExportFormat, string> FormatDescriptions =
    new Dictionary<ExportFormat,string>()
{
    { ExportFormat.Csv, "Comma Separated Values" },
    { ExportFormat.Tsv, "Tab Separated Values" },
    { ExportFormat.Excel, "Microsoft Excel 2007" },            
};

public static string Describe(this ExportFormat e)
{
    var formats = e.Formats();
    var descriptions = formats.Select(fmt => FormatDescriptions[fmt]);

    return string.Join(", ", descriptions.ToArray());
}
专用静态字典格式说明=
新字典()
{
{ExportFormat.Csv,“逗号分隔值”},
{ExportFormat.Tsv,“制表符分隔值”},
{ExportFormat.Excel,“MicrosoftExcel2007”},
};
公共静态字符串描述(此导出格式为e)
{
var formats=e.formats();
变量描述=格式。选择(fmt=>FormatDescriptions[fmt]);
返回字符串.Join(“,”,descriptions.ToArray());
}
通过这种方式,很容易合并来自外部源或本地化的字符串描述,如上所述。

Dupe:

您可以编写一个类型转换器来读取指定的属性,以便在资源中查找它们。因此,您将获得显示名称的多语言支持,而无需太多匆忙

查看TypeConverter的ConvertFrom/ConvertTo方法,并使用反射读取枚举字段上的属性

添加:

在链接的帖子中向下滚动,查看TypeConverter的实现,该实现完成了完全支持所需的部分功能

这将支持同时使用多种语言的应用程序,而不仅仅是代码名->英文名

请记住,这只是显示名称,而不是存储值。您应该始终存储代码名或整数值,以支持不同地区的用户使用相同的数据。

Dupe:

您可以编写一个类型转换器来读取指定的属性,以便在资源中查找它们。因此,您将获得显示名称的多语言支持,而无需太多匆忙

查看TypeConverter的ConvertFrom/ConvertTo方法,并使用反射读取枚举字段上的属性

添加:

在链接的帖子中向下滚动,查看TypeConverter的实现,该实现完成了完全支持所需的部分功能

这将支持同时使用多种语言的应用程序,而不仅仅是代码名->英文名


请记住,这只是显示名称,而不是存储值。您应该始终存储代码名或整数值,以支持不同地区的用户使用相同的数据。

您不也需要本地化这些字符串吗?如果是这样,它们无论如何都会在资源文件中,因此没有必要将它们放在code.True中的任何位置。但是仍然需要某种方式将资源键与枚举连接起来。您不也需要本地化这些字符串吗?如果是这样,它们无论如何都会在资源文件中,因此没有必要将它们放在code.True中的任何位置。但仍然需要某种方式将资源密钥与枚举连接起来。您可能希望在运行时缓存查找,因为它们不会更改,并且使用反射来描述重复调用的成本会很高。但这很难本地化,不是吗?因为据我所知,在使用属性时,您无法真正查找资源字符串等。您可能希望在运行时缓存查找,因为它们不会更改,并且使用反射来重复调用来描述会很昂贵。但这很难本地化,不是吗