资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Enum扩展特性的示例分析-创新互联

这篇文章将为大家详细讲解有关Enum扩展特性的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

渭源ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联建站的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!

使用特性代替了直接使用中文作为属性。特意摘抄部分为以后使用方便

    /// /// 枚举帮助类/// public static class EnumTools
    {/// ///  获取当前枚举值的描述和排序/// /// /// 返回元组Tuple(string,int)public static Tuple GetDescription(this Enum value)
        {int order = 0;string description = string.Empty;

            Type type = value.GetType();// 获取枚举FieldInfo fieldInfo = type.GetField(value.ToString());// 获取枚举自定义的特性DescriptionAttributeobject[] attrs = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
            DescriptionAttribute attr = (DescriptionAttribute)attrs.FirstOrDefault(a => a is DescriptionAttribute);

            description = fieldInfo.Name;if (attr != null)
            {
                order = attr.Order;
                description = attr.Name;
            }return new Tuple(description,order);

        }/// /// 获取当前枚举的所有描述/// /// public static List> GetAll()
        {return GetAll(typeof(T));
        }/// /// 获取所有的枚举描述和值/// /// /// public static List> GetAll(Type type)
        {

            List list = new List();// 循环枚举获取所有的Fieldsforeach (var field in type.GetFields())
            {// 如果是枚举类型if (field.FieldType.IsEnum)
                {object tmp = field.GetValue(null);
                    Enum enumValue = (Enum)tmp;int intValue = Convert.ToInt32(enumValue);var dec = enumValue.GetDescription();int order= dec.Item2;string showName = dec.Item1; // 获取描述和排序list.Add(new EnumToolsModel { Key = intValue, Name = showName, Order = order });
                }
            }// 排序并转成KeyValue返回return list.OrderBy(i => i.Order).Select(i => new KeyValuePair(i.Key, i.Name)).ToList();

        }/// /// 枚举Model///  partial class EnumToolsModel
        {public int Order { get; set; }public string Name { get; set; }public int Key { get; set; }
        }

    }/// /// 枚举特性/// [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]public class DescriptionAttribute : Attribute
    {/// /// 排序/// public int Order { get; set; }/// /// 名称/// public string Name { get; set; }/// /// 定义描述名称/// /// 名称public DescriptionAttribute(string name)
        {
            Name = name;
        }/// /// 定义描述名称和排序/// /// 名称/// 排序public DescriptionAttribute(string name, int order)
        {
            Name = name;
            Order = order;
        }

    }

把原文中的out参数替换成返回元组,由于项目是vs2015开发,不能用c#7.0特性,否则用7.0中的值元组应该更好一点。性能和显示友好性都会有改进。

关于“Enum扩展特性的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


文章名称:Enum扩展特性的示例分析-创新互联
网站URL:http://cdkjz.cn/article/ccjgdh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220