.NET獲取枚舉DescriptionAttribute描述信息性能改進(jìn)的多種方法
一. DescriptionAttribute的普通使用方式
1.1 使用示例
DescriptionAttribute特性可以用到很多地方,比較常見(jiàn)的就是枚舉,通過(guò)獲取枚舉上定義的描述信息在UI上顯示,一個(gè)簡(jiǎn)單的枚舉定義:
public enum EnumGender
{
None,
[System.ComponentModel.Description("男")]
Male,
[System.ComponentModel.Description("女")]
Female,
Other,
}
本文不討論DescriptionAttribute的其他應(yīng)用場(chǎng)景,也不關(guān)注多語(yǔ)言的實(shí)現(xiàn),只單純的研究下獲取枚舉描述信息的方法。
一般比較常見(jiàn)的獲取枚舉描述信息的方法如下,可以在園子里搜索類(lèi)似的代碼非常多。
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;
}
簡(jiǎn)單測(cè)試下:
Console.WriteLine(EnumGender.Female.GetDescriptionOriginal()); Console.WriteLine(EnumGender.Male.GetDescriptionOriginal()); Console.WriteLine(EnumGender.Other.GetDescriptionOriginal()); //輸出結(jié)果: 女 男 Other
1.2 上面的實(shí)現(xiàn)代碼的問(wèn)題
首先要理解特性是什么?
特性:
Attribute特性就是關(guān)聯(lián)了一個(gè)目標(biāo)對(duì)象的一段配置信息,存儲(chǔ)在dll內(nèi)的元數(shù)據(jù)。它本身沒(méi)什么意義,可以通過(guò)反射來(lái)獲取配置的特性信息。
因此主要問(wèn)題其實(shí)就是反射造成的嚴(yán)重性能問(wèn)題:
•1.每次調(diào)用都會(huì)使用反射,效率慢!
•2.每次調(diào)用反射都會(huì)生成新的DescriptionAttribute對(duì)象,哪怕是同一個(gè)枚舉值。造成內(nèi)存、GC的極大浪費(fèi)!
•3.好像不支持位域組合對(duì)象!
•4.這個(gè)地方的方法參數(shù)是Enum,Enum是枚舉的基類(lèi),他是一個(gè)引用類(lèi)型,而枚舉是值類(lèi)型,該方法會(huì)造成裝箱,不過(guò)這個(gè)問(wèn)題好像是不可避免的。
性能到底有多差呢?代碼來(lái)實(shí)測(cè)一下:
[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();
}
});
});
}
//輸出結(jié)果:
80
TimeSpan:79,881.0000ms //共消耗了將近80秒
MemoryUsed:-1,652.7970KB
CollectionCount(0):7,990.00 //0代GC回收了7千多次,因?yàn)閯?chuàng)建了大量的DescriptionAttribute對(duì)象
其中this.GetTestEnums();方法使用獲取一個(gè)枚舉值集合,用于測(cè)試的,集合大小80,執(zhí)行100w次,相當(dāng)于執(zhí)行了8000w次GetDescriptionOriginal方法。
TestHelper.InvokeAndWriteAll方法是用來(lái)計(jì)算執(zhí)行前后的時(shí)間、內(nèi)存消耗、0代GC回收次數(shù)的,文末附錄中給出了代碼,由于內(nèi)存回收的原因,內(nèi)存消耗計(jì)算其實(shí)不準(zhǔn)確的,不過(guò)可以參考第三個(gè)指標(biāo)0代GC回收次數(shù)。
二. 改進(jìn)的DescriptionAttribute方法
知道了問(wèn)題原因,解決就好辦了,基本思路就是把獲取到的文本值緩存起來(lái),一個(gè)枚舉值只反射一次,這樣性能問(wèn)題就解決了。
2.1 使用字典緩存+鎖
因?yàn)槭褂渺o態(tài)變量字典來(lái)緩存值,就涉及到線程安全,需要使用鎖(做了雙重檢測(cè)),具體方法:
private static Dictionary<Enum, string> _LockDictionary = new Dictionary<Enum, 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];
}
來(lái)測(cè)試一下,測(cè)試數(shù)據(jù)、次數(shù)和1.2的GetDescriptionOriginal_Test相同,效率有很大的提升,只有一次內(nèi)存回收。
[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();
}
});
});
}
//測(cè)試結(jié)果:
80
TimeSpan:1,860.0000ms
MemoryUsed:159.2422KB
CollectionCount(0):1.00
2.2 使用字典緩存+異常(不走尋常路的方式)
還是先看看實(shí)現(xiàn)方法吧!
private static Dictionary<Enum, string> _ExceptionDictionary = new Dictionary<Enum, 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];
}
}
假設(shè)我們的使用場(chǎng)景是這樣的:項(xiàng)目定義的枚舉并不多,但是用其描述值很頻繁,比如定義了一個(gè)用戶(hù)性別枚舉,用的地方很多,使用頻率很高。
上面GetDescriptionByDictionaryWithLocak的方法中,第一句代碼“if (_LockDictionary.ContainsKey(@this)) ”就是驗(yàn)證是否包含枚舉值。在2.1的測(cè)試中執(zhí)行了8000w次,其中只有80次(總共只有80個(gè)枚舉值用于測(cè)試)需要這句代碼“if (_LockDictionary.ContainsKey(@this)) ”,其余的直接取值就可了?;谶@樣的考慮,就有了上面的方法GetDescriptionByDictionaryWithException。
來(lái)測(cè)試一下,看看效果吧!
[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();
}
});
});
}
//測(cè)試結(jié)果:
80
TimeSpan:1,208.0000ms
MemoryUsed:230.9453KB
CollectionCount(0):1.00
測(cè)試結(jié)果來(lái)看,基本上差不多,在時(shí)間上略微快樂(lè)一點(diǎn)點(diǎn),1,208.0000ms:1,860.0000ms,執(zhí)行8000w次快600毫秒,好像差別也不大啊,這是為什么呢?
這個(gè)其實(shí)就是Dictionary的問(wèn)題了,Dictionary內(nèi)部使用散列算法計(jì)算存儲(chǔ)地址,其查找的時(shí)間復(fù)雜度為o(1),他的查找效果是非??斓模痉椒ㄖ欣昧水惓L幚?,異常捕獲本身是有一定性能影響的。
2.3 推薦簡(jiǎn)單方案:ConcurrentDictionary
ConcurrentDictionary是一個(gè)線程安全的字典類(lèi),代碼:
private static ConcurrentDictionary<Enum, string> _ConcurrentDictionary = new ConcurrentDictionary<Enum, 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);
});
}
測(cè)試代碼及測(cè)試結(jié)果:
[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();
}
});
});
}
//測(cè)試結(jié)果:
80
TimeSpan:1,303.0000ms
MemoryUsed:198.0859KB
CollectionCount(0):1.00
2.4 正式的代碼
綜上所述,解決了性能問(wèn)題、位域枚舉問(wèn)題的正式的代碼:
/// <summary>
/// 獲取枚舉的描述信息(Descripion)。
/// 支持位域,如果是位域組合值,多個(gè)按分隔符組合。
/// </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則應(yīng)該是組合位域值,
return field == null ? key.GetDescriptions() : GetDescription(field);
});
}
/// <summary>
/// 獲取位域枚舉的描述,多個(gè)按分隔符組合
/// </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 );
相關(guān)文章
ASP.NET Core 2.0 帶初始參數(shù)的中間件問(wèn)題及解決方法
這篇文章主要介紹了ASP.NET Core 2.0 帶初始參數(shù)的中間件問(wèn)題及解決方法,需要的朋友可以參考下2017-10-10
asp.net(c#)獲取內(nèi)容第一張圖片地址的函數(shù)
C#獲取文章類(lèi)第一張圖片的地址的函數(shù)(留著以后用),先說(shuō)一下思路2009-11-11
asp.net+xml+flash實(shí)現(xiàn)的圖片展示效果示例
這篇文章主要介紹了asp.net+xml+flash實(shí)現(xiàn)的圖片展示效果的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了圖片展示效果的相關(guān)操作步驟與flash與xml調(diào)用的相關(guān)技巧,需要的朋友可以參考下2016-08-08
ASP.NET Core單文件和多文件上傳并保存到服務(wù)端的方法
這篇文章主要介紹了ASP.NET Core單文件和多文件上傳并保存到服務(wù)端的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
ASP.NET CORE學(xué)習(xí)教程之自定義異常處理詳解
這篇文章主要給大家介紹了關(guān)于ASP.NET CORE學(xué)習(xí)教程之自定義異常處理的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
ASP.NET+Web服務(wù)實(shí)現(xiàn)軟件共享
ASP.NET+Web服務(wù)實(shí)現(xiàn)軟件共享...2006-09-09
Visual Studio for Mac版 初體驗(yàn)
這篇文章主要介紹了Visual Studio for Mac版 初體驗(yàn),本文圖文并茂給大家介紹的非常詳細(xì),需要的朋友可以參考下2017-05-05
js獲取.aspx頁(yè)面里面的服務(wù)器控件和.ascx中的服務(wù)器控件值
用js獲取.ascx控件中服務(wù)器控件值時(shí)首先要得到服務(wù)器控件的ClientID再加上.ascx頁(yè)面里面的服務(wù)器空間ID并用"_"連接2009-02-02
如何使用Microsoft.Extensions.AI簡(jiǎn)化.NET中的AI集成
Microsoft.Extensions.AI是一個(gè)創(chuàng)新的?.NET?庫(kù),它為平臺(tái)開(kāi)發(fā)人員提供了一個(gè)內(nèi)聚的?C#?抽象層,簡(jiǎn)化了與大型語(yǔ)言模型(LLMs)和嵌入等AI服務(wù)的交互,本文給大家介紹如何使用Microsoft.Extensions.AI簡(jiǎn)化.NET中的AI集成,感興趣的朋友一起看看吧2024-11-11

