简体   繁体   English

具有标志属性的友好枚举字符串

[英]Friendly Enum Strings With Flags Attribute

After perusing some other questions regarding common ways to generically create access to friendly strings for enumeration values in C# this answer appeared to be my best bet for a generic solution where the friendly strings can be placed in the definition of the enumeration using DescriptionAttribute s. 在仔细研究了一些其他有关在C#中通用创建对枚举值的友好字符串访问方式的常见问题之后, 此答案似乎是我对通用解决方案的最佳选择,在通用解决方案中,可以使用DescriptionAttribute将友好字符串放入枚举的定义中。

I implemented this as an extension method, but quickly realized that it would only work for standard enums, where the [Flags] attribute is not specified. 我将其实现为扩展方法,但很快意识到,它仅适用于未指定[Flags]属性的标准枚举。 I'm not completely sure of the best way to pursue implementing this for cases where the attribute is present. 对于属性存在的情况,我不确定要实现此目标的最佳方法。

Since the flags attribute means that multiple values can be selected simultaneously, using a single "friendly string" would not make sense. 由于flags属性意味着可以同时选择多个值,因此使用单个“友好字符串”将毫无意义。 I was thinking of defining the friendly strings in the same way, but overloading the extension method to take that specific enum type where it would return a List<string> to provide friendly strings for all of the selected values. 我正在考虑以相同的方式定义友好字符串,但是重载了扩展方法以采用特定的枚举类型,该枚举类型将返回List<string>为所有选定值提供友好字符串。

The solution described above would work, but I feel like there will be lots of code duplication since each enum that uses the Flags attribute will require it's own extension method because enums can only be inherited by System.Enum , eliminating my ability to create a base type. 上面描述的解决方案可以工作,但是我感觉会有很多代码重复,因为每个使用Flags属性的枚举都将需要它自己的扩展方法,因为枚举只能由System.Enum继承,从而消除了我创建基数的能力类型。 It would be nicer if I could have a more generic method that can handle this by checking the enum if the flags attribute is present and then return one of the following: 如果我可以有一个更通用的方法来处理此问题,那就更好了,该方法可以通过检查是否存在flags属性来枚举枚举,然后返回以下值之一:

  • single string (if no Flags), list (if Flags) - signature returns object 单字符串(如果没有标志),列表(如果标志)-签名返回object
  • single string (if no Flags), list (if Flags) - signature returns dynamic 单字符串(如果没有标志),列表(如果标志)-签名返回dynamic
  • list that may only contain one value if flags is not specified - signature returns List<string> 如果未指定标志,则列表只能包含一个值-签名返回List<string>

I feel like this question may be a case of "having my cake and eating it too", since I would prefer to not have to do additional checks after getting the friendly string(s) and deduping my code. 我觉得这个问题可能是“也要吃蛋糕”的情况,因为我希望在获取友好的字符串并删除我的代码后不必进行其他检查。 Is there a trick or good way to do this that isn't a messy hack? 有没有技巧而不是杂乱无章的技巧?

You can write a method just as in the answer you link, but with support for flag enums, and return a comma seperated string, something like: 您可以像在链接的答案中一样编写方法,但是支持标志枚举,并返回逗号分隔的字符串,例如:

public static string GetDescription(Enum value)
{
    Type type = value.GetType();
    var values = Enum.GetValues(type);
    var setValues = new List<Enum>();
    foreach(var enumValue in values)
    {
        if (value.HasFlag((Enum)enumValue))
            setValues.Add((Enum)enumValue);
    }
    var stringList = new List<string>();
    foreach (var singleValue in setValues)
    {
        var name = Enum.GetName(type, singleValue);
        if (name != null)
        {
            FieldInfo field = type.GetField(name);
            if (field != null)
            {
                DescriptionAttribute attr =
                       Attribute.GetCustomAttribute(field,
                         typeof(DescriptionAttribute)) as DescriptionAttribute;
                if (attr != null)
                {
                    stringList.Add(attr.Description);
                }
            }
        }
    }
    return string.Join(",", stringList.ToArray());
}

not the cleanest code, but you get the idea, only keep in my mind, that it wont work as expected for enums that are not flags - just throwing an idea. 不是最干净的代码,但是您会明白,只是记住我的想法,即对于不是标记的枚举,它将无法按预期工作-只是抛出一个想法。

Use enum.ToString() to get "unfriendly" strings (where enum is your Enum variable). 使用enum.ToString()获得“不友好的”字符串(其中enum是您的Enum变量)。 Write a reusable extension method to convert UnfriendlyString to a friendly "unfriendly string" (eg insert space-lowercase wherever there is an uppercase or something similar). 编写一个可重用的扩展方法,将UnfriendlyString转换为友好的“ unfriendly string”(例如,在存在大写或类似内容的地方插入空格小写)。

For [Flags] you could either Split the unfriendly string, convert each sub-string, and perhaps Join again; 对于[Flags]您可以Split不友好的字符串,转换每个子字符串,也可以再次Join or your extension method could take the commas into account. 否则您的扩展方法可能会将逗号考虑在内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM