HTML5技术

.NET获取枚举DescriptionAttribute描述信息性能改进的多种方法 - /*梦里花落知多少*/(3)

字号+ 作者:H5之家 来源:H5之家 2016-01-15 08:14 我要评论( )

EnumExtension{ 获取枚举的描述信息(Descripion)。 /// 支持位域,如果是位域组合值,多个按分隔符组合。 GetDescription( this Enum @this){ return _ConcurrentDictionary.GetOrAdd(@this, (key) = { var type =

EnumExtension { 获取枚举的描述信息(Descripion)。 /// 支持位域,如果是位域组合值,多个按分隔符组合。 GetDescription(this Enum @this) { return _ConcurrentDictionary.GetOrAdd(@this, (key) => { var type = key.GetType(); var field = type.GetField(key.ToString()); field == null ? key.GetDescriptions() : GetDescription(field); }); } 获取位域枚举的描述,多个按分隔符组合 GetDescriptions() { ); 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); } GetDescription(FieldInfo field) { var att = System.Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute), false); return att == null ? field.Name : ((DescriptionAttribute)att).Description; } 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; } private static Dictionary<Enum, string> _LockDictionary = new Dictionary<Enum, 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]; } private static Dictionary<Enum, string> _ExceptionDictionary = new Dictionary<Enum, 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]; } } _obj = new object(); private static ConcurrentDictionary<Enum, string> _ConcurrentDictionary = new ConcurrentDictionary<Enum, 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); }); } }

View Code

2.测试类EnumTest.cs代码: 

[TestFixture] public class EnumTest { [Test] public void SimpleTest() { Console.WriteLine(EnumGender.Female.GetDescriptionOriginal()); Console.WriteLine(EnumGender.Male.GetDescriptionOriginal()); Console.WriteLine(EnumGender.Other.GetDescriptionOriginal()); var t1 = EnumGender.Male | EnumGender.Female; Console.WriteLine((t1 & EnumGender.Male) == EnumGender.Male); Console.WriteLine(t1 & ~EnumGender.Male); Console.WriteLine(Enum.IsDefined(typeof(EnumGender), 0)); } [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(); } }); }); } [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(); } }); }); } [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(); } }); }); } [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(); } }); }); } private List<Enum> GetTestEnums() { List<Enum> res = new List<Enum>(); res.Add(EnumMutliFTest.T1); res.Add(EnumMutliFTest.T2); res.Add(EnumMutliFTest.T3); res.Add(EnumMutliFTest.T4); res.Add(EnumMutliFTest.T5); res.Add(EnumMutliFTest.T6); res.Add(EnumMutliFTest.T7); res.Add(EnumMutliFTest.T8); res.Add(EnumMutliFTest.T9); res.Add(EnumMutliFTest.T10); res.Add(EnumMutliFTest.T11); res.Add(EnumMutliFTest.T12); res.Add(EnumMutliFTest.T13); res.Add(EnumMutliFTest.T14); res.Add(EnumMutliFTest.T15); res.Add(EnumMutliFTest.T16); res.Add(EnumMutliFTest.T17); res.Add(EnumMutliFTest.T18); res.Add(EnumMutliFTest.T19); res.Add(EnumMutliFTest.T20); res.Add(EnumMutliFTest.T21); res.Add(EnumMutliFTest.T22); res.Add(EnumMutliFTest.T23); res.Add(EnumMutliFTest.T24); res.Add(EnumMutliFTest.T25); res.Add(EnumMutliFTest.T26); res.Add(EnumMutliFTest.T27); res.Add(EnumMutliFTest.T28); res.Add(EnumMutliFTest.T29); res.Add(EnumMutliFTest.T30); res.Add(EnumMutliFTest.T31); res.Add(EnumMutliFTest.T32); res.Add(EnumMutliFTest.T33); res.Add(EnumMutliFTest.T34); res.Add(EnumMutliFTest.T35); res.Add(EnumMutliFTest.T36); res.Add(EnumMutliFTest.T37); res.Add(EnumMutliFTest.T38); res.Add(EnumMutliFTest.T3); res.Add(EnumMutliFTest.T18); res.Add(EnumMutliFTest2.T21); res.Add(EnumMutliFTest2.T22); res.Add(EnumMutliFTest2.T23); res.Add(EnumMutliFTest2.T24); res.Add(EnumMutliFTest2.T25); res.Add(EnumMutliFTest2.T26); res.Add(EnumMutliFTest2.T27); res.Add(EnumMutliFTest2.T28); res.Add(EnumMutliFTest2.T29); res.Add(EnumMutliFTest2.T210); res.Add(EnumMutliFTest2.T211); res.Add(EnumMutliFTest2.T212); res.Add(EnumMutliFTest2.T213); res.Add(EnumMutliFTest2.T214); res.Add(EnumMutliFTest2.T215); res.Add(EnumMutliFTest2.T216); res.Add(EnumMutliFTest2.T217); res.Add(EnumMutliFTest2.T218); res.Add(EnumMutliFTest2.T219); res.Add(EnumMutliFTest2.T220); res.Add(EnumMutliFTest2.T221); res.Add(EnumMutliFTest2.T222); res.Add(EnumMutliFTest2.T223); res.Add(EnumMutliFTest2.T224); res.Add(EnumMutliFTest2.T225); res.Add(EnumMutliFTest2.T226); res.Add(EnumMutliFTest2.T227); res.Add(EnumMutliFTest2.T228); res.Add(EnumMutliFTest2.T229); res.Add(EnumMutliFTest2.T230); res.Add(EnumMutliFTest2.T231); res.Add(EnumMutliFTest2.T232); res.Add(EnumMutliFTest2.T233); res.Add(EnumMutliFTest2.T234); res.Add(EnumMutliFTest2.T235); res.Add(EnumMutliFTest2.T236); res.Add(EnumMutliFTest2.T237); res.Add(EnumMutliFTest2.T238); res.Add(EnumMutliFTest2.T23); res.Add(EnumMutliFTest2.T218); return res; } public enum EnumMutliFTest { [System.ComponentModel.Description()] T1, [System.ComponentModel.Description()] T2, [System.ComponentModel.Description()] T3, [System.ComponentModel.Description()] T4, [System.ComponentModel.Description()] T5, [System.ComponentModel.Description()] T6, [System.ComponentModel.Description()] T7, [System.ComponentModel.Description()] T8, [System.ComponentModel.Description()] T9, [System.ComponentModel.Description()] T10, [System.ComponentModel.Description()] T11, [System.ComponentModel.Description()] T12, [System.ComponentModel.Description()] T13, [System.ComponentModel.Description()] T14, [System.ComponentModel.Description()] T15, [System.ComponentModel.Description()] T16, [System.ComponentModel.Description()] T17, [System.ComponentModel.Description()] T18, [System.ComponentModel.Description()] T19, [System.ComponentModel.Description()] T20, [System.ComponentModel.Description()] T21, [System.ComponentModel.Description()] T22, [System.ComponentModel.Description()] T23, [System.ComponentModel.Description()] T24, [System.ComponentModel.Description()] T25, [System.ComponentModel.Description()] T26, [System.ComponentModel.Description()] T27, [System.ComponentModel.Description()] T28, [System.ComponentModel.Description()] T29, [System.ComponentModel.Description()] T30, [System.ComponentModel.Description()] T31, [System.ComponentModel.Description()] T32, [System.ComponentModel.Description()] T33, [System.ComponentModel.Description()] T34, [System.ComponentModel.Description()] T35, [System.ComponentModel.Description()] T36, [System.ComponentModel.Description()] T37, [System.ComponentModel.Description()] T38, } public enum EnumMutliFTest2 { [System.ComponentModel.Description()] T21, [System.ComponentModel.Description()] T22, [System.ComponentModel.Description()] T23, [System.ComponentModel.Description()] T24, [System.ComponentModel.Description()] T25, [System.ComponentModel.Description()] T26, [System.ComponentModel.Description()] T27, [System.ComponentModel.Description()] T28, [System.ComponentModel.Description()] T29, [System.ComponentModel.Description()] T210, [System.ComponentModel.Description()] T211, [System.ComponentModel.Description()] T212, [System.ComponentModel.Description()] T213, [System.ComponentModel.Description()] T214, [System.ComponentModel.Description()] T215, [System.ComponentModel.Description()] T216, [System.ComponentModel.Description()] T217, [System.ComponentModel.Description()] T218, [System.ComponentModel.Description()] T219, [System.ComponentModel.Description()] T220, [System.ComponentModel.Description()] T221, [System.ComponentModel.Description()] T222, [System.ComponentModel.Description()] T223, [System.ComponentModel.Description()] T224, [System.ComponentModel.Description()] T225, [System.ComponentModel.Description()] T226, [System.ComponentModel.Description()] T227, [System.ComponentModel.Description()] T228, [System.ComponentModel.Description()] T229, [System.ComponentModel.Description()] T230, [System.ComponentModel.Description()] T231, [System.ComponentModel.Description()] T232, [System.ComponentModel.Description()] T233, [System.ComponentModel.Description()] T234, [System.ComponentModel.Description()] T235, [System.ComponentModel.Description()] T236, [System.ComponentModel.Description()] T237, [System.ComponentModel.Description()] T238, } //['dend] [Flags] public enum EnumGender { None, [System.ComponentModel.Description()] Male, [System.ComponentModel.Description()] Female, Other, } }

View Code

3.辅助测试类TestHelper.cs

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • Dora.Interception: 一个为.NET Core度身定制的AOP框架 - Artech

    Dora.Interception: 一个为.NET Core度身定制的AOP框架 - Artech

    2017-05-02 11:00

  • 如何在 ASP.NET Core 中发送邮件 - Savorboard

    如何在 ASP.NET Core 中发送邮件 - Savorboard

    2017-05-02 08:02

  • 十二个 ASP.NET Core 例子 - Savorboard

    十二个 ASP.NET Core 例子 - Savorboard

    2017-04-27 16:01

  • ASP.NET MVC5请求管道和生命周期 - 雪飞鸿

    ASP.NET MVC5请求管道和生命周期 - 雪飞鸿

    2017-04-24 08:04

网友点评