从标记的枚举中获取描述属性 [英] Get Description Attributes From a Flagged Enum

查看:83
本文介绍了从标记的枚举中获取描述属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个扩展方法,该方法将返回包含所有 Description 属性的 List< string> 仅用于给定 [Flags]枚举的设置值。



例如,假设我有以下枚举在我的C#代码中声明:

  [标记] 
公共枚举结果
{
[ Description(值1含空格)]
Value1 = 1,
[Description(值2含空格)]
Value2 = 2,
[Description( Value 3(带空格)]
Value3 = 4,
[Description(值4(带空格))
Value4 = 8
}

然后将变量设置为:

 结果y = Result.Value1 | Result.Value2 | Result.Value4; 

因此,我要创建的呼叫为:

  List< string> descriptions = y.GetDescriptions(); 

,最终结果将是:

  descriptions = {带有空格的值1,带有空格的值2,带有空格的值4}; 

我创建了一种扩展方法,用于获取单个描述属性不能具有沿以下几行设置的多个标志的枚举:

 公共静态字符串GetDescription(此Enum值)
{
类型类型= value.GetType();
字符串名称= Enum.GetName(类型,值);
if(name!= null)
{
System.Reflection.FieldInfo field = type.GetField(name);
if(field!= null)
{
DescriptionAttribute attr =
Attribute.GetCustomAttribute(field,
typeof(DescriptionAttribute))作为DescriptionAttribute;
if(attr!= null)
{
return attr.Description;
}
}
}
返回null;
}

我在网上找到了一些有关如何获取所有Description属性的答案给定的Enum类型(例如此处),但是我在编写通用扩展方法以返回列表时遇到问题

任何帮助将不胜感激。



谢谢!

解决方案

HasFlag 是您的朋友。 :-)



下面的扩展方法使用您上面发布的GetDescription扩展方法,因此请确保具有该扩展方法。然后,下面的方法应该起作用:

  public static List< string> GetDescriptionsAsText(此枚举yourEnum)
{
List< string> descriptions = new List< string>();

foreach(Enum.GetValues(yourEnum.GetType()中的Enum enumValue))
{
if(yourEnum.HasFlag(enumValue))
{
descriptions.Add(enumValue.GetDescription());
}
}

返回说明;
}

注意 HasFlag 允许您将给定的Enum值与定义的标志进行比较。在您的示例中,如果您拥有

 结果y = Result.Value1 | Result.Value2 | Result.Value4; 

然后

  y.HasFlag(Result.Value1)

应为true,而

  y.HasFlag(Result.Value3)

将为错误。



另请参阅: https://msdn.microsoft.com/zh-cn/library/system.enum.hasflag(v = vs.110)。 aspx


I am trying to create an extension method that will return a List<string> containing all the Description attributes for only the set values of a given [Flags] Enum.

For example, suppose I have the following enum declared in my C# code:

[Flags]
public enum Result
{
    [Description("Value 1 with spaces")]
    Value1 = 1,
    [Description("Value 2 with spaces")]
    Value2 = 2,
    [Description("Value 3 with spaces")]
    Value3 = 4,
    [Description("Value 4 with spaces")]
    Value4 = 8
}

And then have a variable set as:

Result y = Result.Value1 | Result.Value2 | Result.Value4;

So, the call I want to create would be:

List<string> descriptions = y.GetDescriptions();

and the final result would be:

descriptions = { "Value 1 with spaces", "Value 2 with spaces", "Value 4 with spaces" };

I have created an extension method for getting the single description attribute for an Enum that can not have multiple flags set that is along the following lines:

public static string GetDescription(this Enum value)
{
    Type type = value.GetType();
    string name = Enum.GetName(type, value);
    if (name != null)
    {
        System.Reflection.FieldInfo field = type.GetField(name);
        if (field != null)
        {
            DescriptionAttribute attr =
                   Attribute.GetCustomAttribute(field,
                     typeof(DescriptionAttribute)) as DescriptionAttribute;
            if (attr != null)
            {
                return attr.Description;
            }
        }
    }
    return null;
}

And I've found some answers online on how to get ALL the Description attributes for a given Enum type (such as here), but I'm having problems writing a generic extension method to return the list of descriptions for only the set attributes.

Any help would be really appreciated.

THANKS!!

解决方案

HasFlag is your friend. :-)

The extension method below uses the GetDescription extension method you've posted above, so ensure you have that. The following should then work:

public static List<string> GetDescriptionsAsText(this Enum yourEnum)
{       
    List<string> descriptions = new List<string>();

    foreach (Enum enumValue in Enum.GetValues(yourEnum.GetType()))
    {
        if (yourEnum.HasFlag(enumValue))
        {
            descriptions.Add(enumValue.GetDescription());
        }
    }

    return descriptions;
}

Note: HasFlag allows you to compare a given Enum value against the flags defined. In your example, if you have

Result y = Result.Value1 | Result.Value2 | Result.Value4;

then

y.HasFlag(Result.Value1)

should be true, while

y.HasFlag(Result.Value3)

will be false.

See also: https://msdn.microsoft.com/en-us/library/system.enum.hasflag(v=vs.110).aspx

这篇关于从标记的枚举中获取描述属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆