zoukankan      html  css  js  c++  java
  • .Net5

    枚举命名规则:

    1、 枚举以复数的形式命名 => 例如性别  public enum Genders { }

    2、状态 以Status结尾

    枚举的扩展方法:还有其他博文
    using System;
    using System.ComponentModel;
    using System.Reflection;
    
    namespace EnumDemo
    {
        /// <summary>
        /// 枚举扩展方法
        /// </summary>
        public static class EnumExtensions
        {
            /// <summary>
            /// 根据枚举int值获取枚举名称
            /// </summary>
            /// <typeparam name="T">枚举类型</typeparam>
            /// <param name="status">枚举值</param>
            /// <returns></returns>
            public static string GetEnumName<T>(this int status)
            {
                return Enum.GetName(typeof(T), status);
            }
    
    
            /// <summary>
            /// 获取枚举变量值的 Description 属性
            /// </summary>
            /// <param name="obj">枚举变量</param>
            /// <returns>如果包含 Description 属性,则返回 Description 属性的值,否则返回枚举变量值的名称</returns>
            public static string GetDescription(this Enum obj)
            {
                return GetDescription(obj, false);
            }
    
            /// <summary>
            /// 获取枚举变量值的 Description 属性
            /// </summary>
            /// <param name="obj">枚举变量</param>
            /// <param name="isTop">是否改变为返回该类、枚举类型的头 Description 属性,而不是当前的属性或枚举变量值的 Description 属性</param>
            /// <returns>如果包含 Description 属性,则返回 Description 属性的值,否则返回枚举变量值的名称</returns>
            public static string GetDescription(this Enum obj, bool isTop)
            {
                if (obj == null)
                {
                    return string.Empty;
                }
                Type enumType = obj.GetType();
                DescriptionAttribute dna;
                if (isTop)
                {
                    dna = (DescriptionAttribute)Attribute.GetCustomAttribute(enumType, typeof(DescriptionAttribute));
                }
                else
                {
                    FieldInfo fi = enumType.GetField(System.Enum.GetName(enumType, obj));
                    dna = (DescriptionAttribute)Attribute.GetCustomAttribute(
                       fi, typeof(DescriptionAttribute));
                }
                if ((dna != null)
                    && (string.IsNullOrEmpty(dna.Description) == false))
                {
                    return dna.Description;
                }
                return obj.ToString();
            }
        }
    }
    

      

    枚举的举例验证:
    using System.ComponentModel;
    
    namespace EnumDemo
    {
        /// <summary>
        /// 枚举以复数的形式命名
        /// </summary>
        public enum Genders
        {
            /// <summary>
            /// 女人
            /// </summary>
            [Description("女人")]
            Woman = 0,
            /// <summary>
            /// 男人
            /// </summary>
            [Description("男人")]
            Man = 1,
            /// <summary>
            /// 未知
            /// </summary>
            [Description("未知")]
            Unknown = 2
        }
    }
    

      

    namespace EnumDemo
    {
        public class EmployeeInfo
        {
            public EmployeeInfo(int empId, string empName, Genders gender)
            {
                EmpId = empId;
                EmpName = empName;
                Gender = gender;
            }
    
            /// <summary>
            /// 编号
            /// </summary>
            public int EmpId { get; set; }
    
            /// <summary>
            /// 姓名
            /// </summary>
            public string EmpName { get; set; }
    
            /// <summary>
            /// 性别
            /// </summary>
            public Genders Gender { get; set; }
    
            /// <summary>
            /// 性别【汉化值】 此属性为只读get
            /// </summary>
            public string GenderName => Gender.GetDescription();
    
            public override string ToString()
            {
                return $"编号:{EmpId}-姓名:{EmpName}-性别:{Gender}-性别【汉化值】:{GenderName}";
            }
        }
    }
    

      

    using System;
    using System.Collections.Generic;
    
    namespace EnumDemo
    {
        class Program
        {
            /// <summary>
            /// 初始化职员信息
            /// </summary>
            /// <returns></returns>
            static IList<EmployeeInfo> CreateEmployeeInfos()
            {
                var employees = new List<EmployeeInfo>
                {
                    new EmployeeInfo(1, "张三", Genders.Man),
                    new EmployeeInfo(2, "李四", Genders.Woman),
                    new EmployeeInfo(3, "王五", Genders.Woman),
                    new EmployeeInfo(4, "陈大", Genders.Man),
                    new EmployeeInfo(5, "钱二", Genders.Unknown)
                };
                return employees;
            }
    
            static void Main(string[] args)
            {
                #region enum、int、string之间的相互转换
                Genders manValue = Genders.Man;// => Man
                //枚举转string
                string manStr = manValue.ToString();//效率低 => Man
                string manStr2 = Enum.GetName(typeof(Genders), manValue);// => Man
                //int转string
                int num = 0;
                string manStr5 = Enum.GetName(typeof(Genders), 0);// => Woman
                string manStr3 = num.GetEnumName<Genders>();// => Woman
                //枚举转int
                int number = (int)manValue;// => 1
                int number2 = manValue.GetHashCode();// => 1
                int number3 = Convert.ToInt32(manValue);// => 1
                //string转枚举
                Genders man = (Genders)Enum.Parse(typeof(Genders), "Man");// => Man
                Genders woman = (Genders)Enum.Parse(typeof(Genders), "0");// => Woman
                //string转int
                int number5 = Convert.ToInt32(Enum.Parse(typeof(Genders), "Man"));// => 1
                //int转枚举
                Genders man2 = (Genders)1;// => Man
                Genders woman2 = (Genders)Enum.ToObject(typeof(Genders), 0);// => Woman
                #endregion
    
                //返回查询数据展示枚举汉语字时
                var employees = CreateEmployeeInfos();
                foreach (var item in employees)
                {
                    Console.WriteLine(item.ToString());
                }
            }
        }
    }
    

      

  • 相关阅读:
    关于JAVA的线程问题
    Java 对JTextField添加回车响应
    Failed to install *.apk on device 'emulator-5554': timeout .
    静态属性
    类与对象的实例属性

    面向对象2
    面向对象设计
    re模块,主要用来查询
    xml对标签操作,
  • 原文地址:https://www.cnblogs.com/gygtech/p/14364794.html
Copyright © 2011-2022 走看看