GVKun编程网logo

c# enum 解析(c#enum类型如何使用)

11

如果您对c#enum解析和c#enum类型如何使用感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解c#enum解析的各种细节,并对c#enum类型如何使用进行深入的分析,此外还有关于c#–‘En

如果您对c# enum 解析c#enum类型如何使用感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解c# enum 解析的各种细节,并对c#enum类型如何使用进行深入的分析,此外还有关于c# – ‘Enum:Type’与’Enum’之间有什么区别?、Enum 和 MaterialEnum 有什么区别?、Enum.valueOf对扩展Enum的未知类型的类抛出警告、Enum.valueOf对扩展Enum的未知类型的类抛出警告。的实用技巧。

本文目录一览:

c# enum 解析(c#enum类型如何使用)

c# enum 解析(c#enum类型如何使用)

解析定义的枚举

public enum OrderPaymentStatus
    {
        /// <summary>
        /// 未支付
        /// </summary>
        [Description("未支付")]
        No=1,
        /// <summary>
        /// 已支付
        /// </summary>
        [Description("已支付")]
        Yes
    }

解析类

public static class EnumHelper
    {
        private static Hashtable enumDesciption = EnumHelper.GetDescriptionContainer();

        public static string ToDescription(this Enum value)
        {
            if (value == null)
            {
                return "";
            }
            Type type = value.GetType();
            string name = Enum.GetName(type, value);
            return EnumHelper.GetDescription(type, name);
        }

        public static Dictionary<int, string> ToDescriptionDictionary<TEnum>()
        {
            Type typeFromHandle = typeof(TEnum);
            Array values = Enum.GetValues(typeFromHandle);
            Dictionary<int, string> dictionary = new Dictionary<int, string>();
            foreach (Enum item in values)
            {
                dictionary.Add(Convert.ToInt32(item), item.ToDescription());
            }
            return dictionary;
        }

        public static Dictionary<int, string> ToDictionary<TEnum>()
        {
            Type typeFromHandle = typeof(TEnum);
            Array values = Enum.GetValues(typeFromHandle);
            Dictionary<int, string> dictionary = new Dictionary<int, string>();
            foreach (Enum item in values)
            {
                dictionary.Add(Convert.ToInt32(item), item.ToString());
            }
            return dictionary;
        }

        private static bool IsIntType(double d)
        {
            return (double)(int)d != d;
        }

        private static Hashtable GetDescriptionContainer()
        {
            EnumHelper.enumDesciption = new Hashtable();
            return EnumHelper.enumDesciption;
        }

        private static void AddToEnumDescription(Type enumType)
        {
            EnumHelper.enumDesciption.Add(enumType, EnumHelper.GetEnumDic(enumType));
        }

        private static Dictionary<string, string> GetEnumDic(Type enumType)
        {
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            FieldInfo[] fields = enumType.GetFields();
            FieldInfo[] array = fields;
            foreach (FieldInfo fieldInfo in array)
            {
                if (fieldInfo.FieldType.IsEnum)
                {
                    object[] customAttributes = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
                    dictionary.Add(fieldInfo.Name, ((DescriptionAttribute)customAttributes[0]).Description);
                }
            }
            return dictionary;
        }

        private static string GetDescription(Type enumType, string enumText)
        {
            if (string.IsNullOrEmpty(enumText))
            {
                return null;
            }
            if (!EnumHelper.enumDesciption.ContainsKey(enumType))
            {
                EnumHelper.AddToEnumDescription(enumType);
            }
            object obj = EnumHelper.enumDesciption[enumType];
            if (obj != null && !string.IsNullOrEmpty(enumText))
            {
                Dictionary<string, string> dictionary = (Dictionary<string, string>)obj;
                return dictionary[enumText].Split(''|'')[0];
            }
            throw new ApplicationException("不存在枚举的描述");
        }
    }

  

c# – ‘Enum:Type’与’Enum’之间有什么区别?

c# – ‘Enum:Type’与’Enum’之间有什么区别?

我试图了解一般关于Enums的一些事情以及他们如何与Chars特别合作.以下是我工作的例子:
public enum AuditInteractionTypes
{
    Authorized = 'A',Created = 'C',Revised = 'R',Extracted = 'E',Deleted = 'D'
}

首先,声明它们枚举AuditInteractionTypes或枚举AuditInteractionTypes:char之间的区别是什么

其次,我看过很多关于尝试使用带有字符的枚举以及如何“使”它来回工作的帖子.可能是愚蠢的问题,但为什么我不能简单地作为一个字符串来回走动.

因此,例如,授权=“A”.

我使用Linq To sql作为我的DAL,如果这很重要,我希望,这是一个更广泛的问题,不是特定于我的环境.

解决方法

它规定了用于存储枚举的基础类型.

当您使用枚举而没有任何其他内容时,它使用int作为底层存储类型.

当您使用enum:< type>时,它使用该类型作为底层存储类型.

在你的情况下,你正在尝试使用char类型的基础类型,但这是无效的,根据the C# reference:

The approved types for an enum are byte,sbyte,short,ushort,int,uint,long,or ulong.

如果要存储char值,则有两个选项.

您可以使用基础类型ushort(它是一个无符号的16位整数,如char),如下所示:

public enum AuditInteractionTypes : ushort
{
    Authorized = 'A',Deleted = 'D'
}

char有一个隐式转换为ushort,所以上面的工作.此外,您可以轻松地比较两者.

如果you want to use a string as the value那么我会推荐类似枚举的类,如下所示:

public static class AuditInteractionTypes
{
    // You can make these static readonly if they are likely to change.
    public const string Authorized = "A";
    public const string Created = "C";
    public const string Revised = "R";
    public const string Extracted = "E";
    public const string Deleted = "D";
}

然后,这个类看起来与enum和代码看起来相同.

注意,任何类型都可以使用相同的技巧,但通常这些类型应该是完全不可变的. string很好地填充了这个准则,完全不可变(如果你正确设计了它们,就像大多数系统值类型和其他值类型一样).

Enum 和 MaterialEnum 有什么区别?

Enum 和 MaterialEnum 有什么区别?

tl;dr:它们确实只是同义词!


查看相应的抽屉类 MaterialPropertyDrawer source code 实际上它在内部称为 MaterialEnumDrawer

但是在 GetShaderPropertyDrawer 中,当他们搜索给定属性 they do

的抽屉实现时
...

// When you write [Foo] in shader,get Foo,FooDrawer,MaterialFooDrawer,// FooDecorator or MaterialFooDecorator class;
// "kind of" similar to how C# does attributes.

if (klass.Name == className ||
    klass.Name == className + "Drawer" ||
    klass.Name == "Material" + className + "Drawer" ||
    klass.Name == className + "Decorator" ||
    klass.Name == "Material" + className + "Decorator")
{

...

因此,正如您所看到的,无论您是否使用 Material 编写它基本上都没有关系,它们基本上被视为同义词:

  • 对于 [Enum],您会得到 EnumEnumDrawerMaterialEnumDrawerEnumDecoratorMaterialEnumDecorator
  • 对于 [MaterialEnum],您会得到 MaterialEnumMaterialEnumDrawerMaterialMaterialEnumDrawerMaterialEnumDecoratorMaterialMaterialEnumDecorator

=>您实际要找的那个MaterialEnumDrawer在两个版本中都能找到。

所以基本上你可以把它写成以下之一

Enum
MaterialEnum
MaterialEnumDrawer

Fazit:使用[Enum]

  • 它更短;)
  • 其他人可以在 API 中找到它

Enum.valueOf对扩展Enum的未知类型的类抛出警告

Enum.valueOf对扩展Enum的未知类型的类抛出警告

给这个:

Class<? extends Enum> enumClass = ...; // being passed in from a constructor
Enum e = Enum.valueOf(enumClass,aString); // produces a warning that looks like

[未检查]未检查的方法调用:java.lang.Enum中的valueOf(java.lang.Class,java.lang.String)应用于(java.lang.Class,java.lang.String)

我不想使用泛型,因为这是一个重大更改。我不想压抑。我不明白为什么会发生此警告。我想这是因为无法扩展Enum类型。我明白了。但是我不明白为什么通配符类会抛出这个奇怪的错误。有没有一种方法可以解决而不使用@SupressWarning或不使用泛型?

编辑 :为澄清起见,以下使用泛型的代码使警告消失。

class Foo<T extends Enum<T>>{
    Class<T> enumClass;
    Enum e = Enum.valueOf(enumClass,aString);
}

的用途<T>是什么,我通过使用泛型的意思。我不能这样做,因为这将是一个巨大的级联变化。

Enum.valueOf对扩展Enum的未知类型的类抛出警告。

Enum.valueOf对扩展Enum的未知类型的类抛出警告。

给这个:

Class<? extends Enum> enumClass = ...; // being passed in from a constructorEnum e = Enum.valueOf(enumClass, aString); // produces a warning that looks like

[未检查]未检查的方法调用:java.lang.Enum中的valueOf(java.lang.Class,java.lang.String)应用于(java.lang.Class,java.lang.String)

我不想使用泛型,因为这是一个重大更改。我不想压抑。我不明白为什么会发生此警告。我想这是因为无法扩展Enum类型。我明白了。但是我不明白为什么通配符类会抛出这个奇怪的错误。有没有一种方法可以解决而不使用@SupressWarning或不使用泛型?

编辑 :为澄清起见,以下使用泛型的代码使警告消失。

class Foo<T extends Enum<T>>{    Class<T> enumClass;    Enum e = Enum.valueOf(enumClass, aString);}

的用途<T>是什么,我通过使用泛型的意思。我不能这样做,因为这将是一个巨大的级联变化。

答案1

小编典典

这似乎是编译器错误-应该是错误,而不是警告。

编译方法调用表达式时Enum.valueOf(enumClass...),首先,将捕获转换应用于参数类型。

<W extends Enum> // a new type parameter Class<W> enumClass; // the type of the argument after capture conversion

然后,对进行类型推断Enum.<T>valueOf(enumClass...),结果为T=W

然后,检查T替换后的边界,即是否W为的子类型Enum<W>

(此过程对于15.12.2.2和15.12.2.3相同;并且15.12.2.7肯定会产生T = W)

在这里,检查应该失败。所有编译器都知道是W的子类型Enum,它不能推断出W是的子类型Enum<W>。(好吧,我们知道这是真的,禁止W=Enum;但是子类型化规则中没有这种知识,因此编译器不会使用它-
我们可以通过在MyEnum层次结构中播放此示例来验证这一点,编译器的行为将相同。)

那么,为什么编译器仅通过警告就通过了边界检查?还有另一个规则,允许从Raw到的分配Raw<X>带有未选中的警告。为什么允许这样做是另一个问题(不应该这样),但是编译器确实可以Raw分配给Raw<X>。显然,此规则被错误地混入了上面的子类型检查步骤,编译器认为既然WEnum,也是某种程度上Enum<W>,编译器仅通过警告就通过了子类型检查,这违反了规范。

如果不应该编译这种方法,正确的方法是什么?我看不到任何东西-只要参数的类型enumClass尚未为的递归形式Class<X extendsEnum<X>>,就没有大量的强制转换/转换可以将其转换为该形式,因此无法匹配Enum.valueOf方法的签名。也许javac家伙故意为了使这种代码编译而违反了规范!

我们今天的关于c# enum 解析c#enum类型如何使用的分享就到这里,谢谢您的阅读,如果想了解更多关于c# – ‘Enum:Type’与’Enum’之间有什么区别?、Enum 和 MaterialEnum 有什么区别?、Enum.valueOf对扩展Enum的未知类型的类抛出警告、Enum.valueOf对扩展Enum的未知类型的类抛出警告。的相关信息,可以在本站进行搜索。

本文标签: