• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    .NET获取枚举DescriptionAttribute描述信息性能改进的多种方法

    一. DescriptionAttribute的普通使用方式

    1.1 使用示例

      DescriptionAttribute特性可以用到很多地方,比较常见的就是枚举,通过获取枚举上定义的描述信息在UI上显示,一个简单的枚举定义:

    public enum EnumGender
    {
    None,
    [System.ComponentModel.Description("男")]
    Male,
    [System.ComponentModel.Description("女")]
    Female,
    Other,
    } 

      本文不讨论DescriptionAttribute的其他应用场景,也不关注多语言的实现,只单纯的研究下获取枚举描述信息的方法。

      一般比较常见的获取枚举描述信息的方法如下,可以在园子里搜索类似的代码非常多。

    public static string GetDescriptionOriginal(this Enum @this)
    {
    var name = @this.ToString();
    var field = @this.GetType().GetField(name);
    if (field == null) return name;
    var att = System.Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute), false);
    return att == null ? field.Name : ((DescriptionAttribute)att).Description;
    }

      简单测试下:

    Console.WriteLine(EnumGender.Female.GetDescriptionOriginal());
    Console.WriteLine(EnumGender.Male.GetDescriptionOriginal());
    Console.WriteLine(EnumGender.Other.GetDescriptionOriginal()); //输出结果: 
    女 
    男 
    Other

    1.2 上面的实现代码的问题

      首先要理解特性是什么?

    特性:

    Attribute特性就是关联了一个目标对象的一段配置信息,存储在dll内的元数据。它本身没什么意义,可以通过反射来获取配置的特性信息。

      因此主要问题其实就是反射造成的严重性能问题:

    •1.每次调用都会使用反射,效率慢!
    •2.每次调用反射都会生成新的DescriptionAttribute对象,哪怕是同一个枚举值。造成内存、GC的极大浪费!
    •3.好像不支持位域组合对象!
    •4.这个地方的方法参数是Enum,Enum是枚举的基类,他是一个引用类型,而枚举是值类型,该方法会造成装箱,不过这个问题好像是不可避免的。

      性能到底有多差呢?代码来实测一下:

    [Test]
    public void GetDescriptionOriginal_Test()
    {
    var enums = this.GetTestEnums();
    Console.WriteLine(enums.Count);
    TestHelper.InvokeAndWriteAll(() =>
    {
    System.Threading.Tasks.Parallel.For(0, 1000000, (i, obj) =>
    {
    foreach (var item in enums)
    {
    var a = item.GetDescriptionOriginal();
    }
    });
    });
    }
    //输出结果:
    80
    TimeSpan:79,881.0000ms //共消耗了将近80秒
    MemoryUsed:-1,652.7970KB
    CollectionCount(0):7,990.00 //0代GC回收了7千多次,因为创建了大量的DescriptionAttribute对象 

      其中this.GetTestEnums();方法使用获取一个枚举值集合,用于测试的,集合大小80,执行100w次,相当于执行了8000w次GetDescriptionOriginal方法。

      TestHelper.InvokeAndWriteAll方法是用来计算执行前后的时间、内存消耗、0代GC回收次数的,文末附录中给出了代码,由于内存回收的原因,内存消耗计算其实不准确的,不过可以参考第三个指标0代GC回收次数。

    二. 改进的DescriptionAttribute方法

      知道了问题原因,解决就好办了,基本思路就是把获取到的文本值缓存起来,一个枚举值只反射一次,这样性能问题就解决了。

    2.1 使用字典缓存+锁

      因为使用静态变量字典来缓存值,就涉及到线程安全,需要使用锁(做了双重检测),具体方法:

    private static DictionaryEnum, string> _LockDictionary = new DictionaryEnum, string>();
    public static string GetDescriptionByDictionaryWithLocak(this Enum @this)
    {
    if (_LockDictionary.ContainsKey(@this)) return _LockDictionary[@this];
    Monitor.Enter(_obj);
    if (!_LockDictionary.ContainsKey(@this))
    {
    var value = @this.GetDescriptionOriginal();
    _LockDictionary.Add(@this, value);
    }
    Monitor.Exit(_obj);
    return _LockDictionary[@this];
    } 

      来测试一下,测试数据、次数和1.2的GetDescriptionOriginal_Test相同,效率有很大的提升,只有一次内存回收。

    [Test]
    public void GetDescriptionByDictionaryWithLocak_Test()
    {
    var enums = this.GetTestEnums();
    Console.WriteLine(enums.Count)
    TestHelper.InvokeAndWriteAll(() =>
    {
    System.Threading.Tasks.Parallel.For(0, 1000000, (i, obj) =>
    {
    foreach (var item in enums)
    {
    var a = item.GetDescriptionByDictionaryWithLocak();
    }
    });
    });
    }
    //测试结果:
    80
    TimeSpan:1,860.0000ms
    MemoryUsed:159.2422KB
    CollectionCount(0):1.00 

    2.2 使用字典缓存+异常(不走寻常路的方式)

      还是先看看实现方法吧!

    private static DictionaryEnum, string> _ExceptionDictionary = new DictionaryEnum, string>();
    public static string GetDescriptionByDictionaryWithException(this Enum @this)
    {
    try
    {
    return _ExceptionDictionary[@this];
    }
    catch (KeyNotFoundException)
    {
    Monitor.Enter(_obj);
    if (!_ExceptionDictionary.ContainsKey(@this))
    {
    var value = @this.GetDescriptionOriginal();
    _ExceptionDictionary.Add(@this, value);
    }
    Monitor.Exit(_obj);
    return _ExceptionDictionary[@this];
    }
    }

      假设我们的使用场景是这样的:项目定义的枚举并不多,但是用其描述值很频繁,比如定义了一个用户性别枚举,用的地方很多,使用频率很高。

      上面GetDescriptionByDictionaryWithLocak的方法中,第一句代码“if (_LockDictionary.ContainsKey(@this)) ”就是验证是否包含枚举值。在2.1的测试中执行了8000w次,其中只有80次(总共只有80个枚举值用于测试)需要这句代码“if (_LockDictionary.ContainsKey(@this)) ”,其余的直接取值就可了。基于这样的考虑,就有了上面的方法GetDescriptionByDictionaryWithException。

      来测试一下,看看效果吧!

    [Test]
    public void GetDescriptionByDictionaryWithException_Test()
    {
    var enums = this.GetTestEnums();
    Console.WriteLine(enums.Count);
    TestHelper.InvokeAndWriteAll(() =>
    {
    System.Threading.Tasks.Parallel.For(0, 1000000, (i, obj) =>
    {
    foreach (var item in enums)
    {
    var a = item.GetDescriptionByDictionaryWithException();
    }
    });
    });
    }
    //测试结果:
    80
    TimeSpan:1,208.0000ms
    MemoryUsed:230.9453KB
    CollectionCount(0):1.00

      测试结果来看,基本上差不多,在时间上略微快乐一点点,1,208.0000ms:1,860.0000ms,执行8000w次快600毫秒,好像差别也不大啊,这是为什么呢?

      这个其实就是Dictionary的问题了,Dictionary内部使用散列算法计算存储地址,其查找的时间复杂度为o(1),他的查找效果是非常快的,而本方法中利用了异常处理,异常捕获本身是有一定性能影响的。

    2.3 推荐简单方案:ConcurrentDictionary

      ConcurrentDictionary是一个线程安全的字典类,代码:

    private static ConcurrentDictionaryEnum, string> _ConcurrentDictionary = new ConcurrentDictionaryEnum, string>();
    public static string GetDescriptionByConcurrentDictionary(this Enum @this)
    {
    return _ConcurrentDictionary.GetOrAdd(@this, (key) =>
    {
    var type = key.GetType();
    var field = type.GetField(key.ToString());
    return field == null ? key.ToString() : GetDescription(field);
    });
    }

      测试代码及测试结果:

    [Test]
    public void GetDescriptionByConcurrentDictionary_Test()
    {
    var enums = this.GetTestEnums();
    Console.WriteLine(enums.Count);
    TestHelper.InvokeAndWriteAll(() =>
    {
    System.Threading.Tasks.Parallel.For(0, 1000000, (i, obj) =>
    {
    foreach (var item in enums)
    {
    var a = item.GetDescriptionByConcurrentDictionary();
    }
    });
    });
    }
    //测试结果:
    80
    TimeSpan:1,303.0000ms
    MemoryUsed:198.0859KB
    CollectionCount(0):1.00 

    2.4 正式的代码

      综上所述,解决了性能问题、位域枚举问题的正式的代码:

    /// summary>
    /// 获取枚举的描述信息(Descripion)。
    /// 支持位域,如果是位域组合值,多个按分隔符组合。
    /// /summary>
    public static string GetDescription(this Enum @this)
    {
    return _ConcurrentDictionary.GetOrAdd(@this, (key) =>
    {
    var type = key.GetType();
    var field = type.GetField(key.ToString());
    //如果field为null则应该是组合位域值,
    return field == null ? key.GetDescriptions() : GetDescription(field);
    });
    }
    /// summary>
    /// 获取位域枚举的描述,多个按分隔符组合
    /// /summary>
    public static string GetDescriptions(this Enum @this, string separator = ",")
    {
    var names = @this.ToString().Split(',');
    string[] res = new string[names.Length];
    var type = @this.GetType();
    for (int i = 0; i  names.Length; i++)
    {
    var field = type.GetField(names[i].Trim());
    if (field == null) continue;
    res[i] = GetDescription(field);
    }
    return string.Join(separator, res);
    }
    private static string GetDescription(FieldInfo field)
    {
    var att = System.Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute), false);
    return att == null ? field.Name : ((DescriptionAttribute)att).Description;
    }
    
    

    ps:.NET获取枚举值的描述

    一、给枚举值定义描述的方式

    public enum TimeOfDay 
    { 
    [Description("早晨")] 
    Moning = 1, 
    [Description("下午")] 
    Afternoon = 2, 
    [Description("晚上")] 
    Evening = 3, 
    } 

    二、获取枚举值的描述的方法

    public static string GetDescriptionFromEnumValue(Type enumType, object enumValue)
    {
    try
    {
    object o = Enum.Parse(enumType, enumValue.ToString());
    string name = o.ToString();
    DescriptionAttribute[] customAttributes = (DescriptionAttribute[])enumType.GetField(name).GetCustomAttributes(typeof(DescriptionAttribute), false);
    if ((customAttributes != null)  (customAttributes.Length == 1))
    {
    return customAttributes[0].Description;
    }
    return name;
    }
    catch
    {
    return "未知";
    }
    }

    三、获取枚举值的描述的方法的使用

    string strMoning = GetDescriptionFromEnumValue( typeof (TimeOfDay) , 2 );
    您可能感兴趣的文章:
    • .NET必知的EventCounters性能指标监视器详解
    • .NET Core/Framework如何创建委托大幅度提高反射调用的性能详解
    • .NET Orm性能测试分析
    • 分享提高ASP.NET Web应用性能的技巧
    • 推荐8项提高 ASP.NET Web API 性能的技术
    • Asp.Net性能优化技巧汇总
    • ASP.NET性能优化八条建议
    • .Net性能调优-ArrayPool详情
    上一篇:ASP.NET中DropDownList和ListBox实现两级联动功能
    下一篇:asp.net实现负载均衡
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    .NET获取枚举DescriptionAttribute描述信息性能改进的多种方法 .NET,获取,枚举,DescriptionAttribute,