繁体   English   中英

枚举中的“&”?

[英]“&” in an enum?

我要消耗一些文件,并且我需要知道每个数据的存放位置。 一栏称为“类别”。

我可以有很多类别,并且我想建立一个枚举来支持它们。

public enum GasKeyWords
       {
           Gas,
           Fuel,
           Gas&Fuel
       }

 public enum EducationKeyWord
 {
    Education,
    Books&Supplies,
    StudentLoan
    Tuition
 }

我在想这样的事情

 foreach(var r in records)
 {
      Categories.GasKeyWords GasKeyWords;

      bool valid = Enum.TryParse<Categories.GasKeyWords>(r, out GasKeyWords);

       if (valid)
        {
                // add to group
        }
 }

并继续使用TryParse,直到找到可以解析的组为止。 我不确定这是否是最好的方法(我一直对更好的想法持开放态度)。

但是,我现在面临的问题是我不能在枚举名称中使用“&”。 我无法控制这些类别名称,因此无法更改。

为什么不使用Description标签解决此问题。 当然,它并不是设计的目的,但是如果您对此有疑问,则可以随时发明自己的属性类。

这是我为获取对象的描述属性而编写的一种快速的肮脏方法

    public static string GetDescription(this object enumeration)
    {
        //Again, not going to do your condition/error checking here.
        //Normally I would have made this extension method for the Enum type
        but you won't know the type at compile time when building the list of descriptions but ..
        // you will know its an object so that is how I cheated around it.

        var desc = enumeration.GetType().GetMember(enumeration.ToString())[0]
            .GetCustomAttributes(typeof(DescriptionAttribute), true) as DescriptionAttribute[];

        if (desc != null && desc.Length > 0)
        {
            return desc[0].Description;
        }

        return enumeration.ToString();
    }

这是一种快速的肮脏实用程序方法,用于将对象的所有描述符的列表作为列表获取

public static IList<string> GetDescriptions<E>(E type)
{
    //I'm not going to do your error checking for you. 
    //Figure out what you want to do in the event that this isn't an enumeration. 
    //Or design it to support different cases. =P

    var list = new List<string>(); 
    foreach(var name in Enum.GetNames(typeof(E)))
    {
        var enumObj = (E) Enum.Parse(typeof(E), name);
        list.Add(enumObj.GetDescription());
    }
    return list;
}

现在要利用这些效果,您需要

   public enum GasKeyWords
   {
       [Description("Gas")] 
       Gas,
       [Description("Fuel")]
       Fuel,
       [Description("Gas&Fuel")]
       GasFuel
   }

   ...


   var gasKeywords = Util.GetDescriptions<GasKeywords>();
   foreach(var r in records)
   {
        var found = gasKeywords.Contains(r);
        if (found)
        {

        }
   }

我不是泛型大师。 如果有人对如何解决GetDescriptions()方法的键入问题有一些想法,那将是受欢迎的。 不幸的是,我无法放入期望Enum的类型约束,这使得它对代码的友好程度降低。

我在自己的项目中做了类似的工作...为了简洁起见...

用法: GetType(Operators).GetTitle(tName))

实施:

 <AttributeUsage(AttributeTargets.Field, AllowMultiple:=False, Inherited:=False)>
    Public Class EnumItemAttribute
        Inherits Attribute

        Public Property Title As String = String.Empty

        Public Property Description As String = String.Empty

    End Class

<Extension()>
    Public Function GetTitle(ByVal tEnum As Type, ByVal tItemName As String) As String

        Dim tFieldInfo As FieldInfo = Nothing

        tFieldInfo = tEnum.GetField(tItemName)
        For Each tAttribute As EnumItemAttribute In tFieldInfo.GetCustomAttributes(GetType(EnumItemAttribute), False)
            Return tAttribute.Title
        Next

        Return String.Empty

    End Function

AC#标识符必须以下划线,Unicode类Lu,Ll,Lt,Lm,Lo或Nl中的字符或其中之一的转义符开头。 所有其他字符必须来自Unicode类Lu,Ll,Lt,Lm,Lo,Nl,Mn,Mc,Nd,Pc或Cf,或其中一个的转义符。

标识符可以以@开头,但这不是标识符的一部分,而是用于允许与关键字相同的单词作为标识符。 因此, myVar@myVar是相同的标识符,而@if是有效的标识符,而我们不能使用if因为它会与关键字冲突。

(C#规范的第2.4.2节)。

&属于Po类,因此未包含在规则中。

对于符合CLS的标识符,ECMA-335要求标识符遵循http://www.unicode.org/reports/tr15/tr15-18.html NFC规范的附件7,且名称不能单独区分大小写。更严格。

简而言之,你不能。

更重要的是,您为什么要这么做? 你怎么能告诉Gas&Fuel从标识符Gas&Fuel具有表达GasFuel作为操作数到&运营商?

暂无
暂无

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

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