C#自定義Attribute值的獲取與優(yōu)化技巧
首先說下什么是Atrribute
首先,我們肯定Attribute是一個類,下面是msdn文檔對它的描述:
公共語言運行時允許你添加類似關鍵字的描述聲明,叫做attributes,它對程序中的元素進行標注,如類型、字段、方法和屬性等。Attributes和Microsoft .NET Framework文件的元數(shù)據(jù)保存在一起,可以用來向運行時描述你的代碼,或者在程序運行的時候影響應用程序的行為。
在.NET中,Attribute被用來處理多種問題,比如序列化、程序的安全特征、防止即時編譯器對程序代碼進行優(yōu)化從而代碼容易調試等等。下面,我們先來看幾個在.NET中標準的屬性的使用,稍后我們再回過頭來討論Attribute這個類本身。(文中的代碼使用C#編寫,但同樣適用所有基于.NET的所有語言)
Attribute作為編譯器的指令
在C#中存在著一定數(shù)量的編譯器指令,如:#define DEBUG, #undefine DEBUG, #if等。這些指令專屬于C#,而且在數(shù)量上是固定的。而Attribute用作編譯器指令則不受數(shù)量限制。比如下面的三個Attribute:
Conditional:起條件編譯的作用,只有滿足條件,才允許編譯器對它的代碼進行編譯。一般在程序調試的時候使用。
DllImport:用來標記非.NET的函數(shù),表明該方法在一個外部的DLL中定義。
Obsolete:這個屬性用來標記當前的方法已經被廢棄,不再使用了。
C# Attribute值獲取
C#自定義Attribute值的獲取是開發(fā)中會經常用到的,一般我們的做法也就是用反射進行獲取的,代碼也不是很復雜。
1、首先有如下自定義的Attribute
[AttributeUsage(AttributeTargets.All)] public sealed class NameAttribute : Attribute { private readonly string _name; public string Name { get { return _name; } } public NameAttribute(string name) { _name = name; } }
2、定義一個使用NameAttribute的類
[Description("Customer Information")] [Name("customer_info")] public class CustomerInfo { [Name("name")] public string Name { get; set; } [Name("address")] public string Address; }
3、獲取CustomAttributes類上的"dept"也就很簡單了
private static string GetName() { var type = typeof(CustomAttributes); var attribute = type.GetCustomAttributes(typeof(NameAttribute), false).FirstOrDefault(); if (attribute == null) { return null; } return ((NameAttribute)attribute).Name; }
以上代碼就可以簡單的獲取,類上的Attribute的值了,但是需求往往不是這么簡單的,不僅要獲取類頭部Attribute上的值,還要獲取字段Address頭部Attribute上的值。有的同學可能就覺得這還不簡單呀,直接上代碼
private static string GetAddress() { var type = typeof (CustomAttributes); var fieldInfo = type.GetField("Address"); if (fieldInfo == null) { return null; } var attribute = fieldInfo.GetCustomAttributes(typeof(NameAttribute), false).FirstOrDefault(); if (attribute == null) { return null; } return ((NameAttribute) attribute).Name; }
上面代碼就是獲取Address字段頭部上的Attribute值了。雖然我們是獲取到了我們想要的,但是我們發(fā)現(xiàn)這樣做是不是太累了,如果又擴展一個自定義的Attribute,或者又在一個新的屬性或字段上標上Attribute時,我們又要寫一段代碼來實現(xiàn)我想要的,這些嚴重代碼違反了DRY的設計原則。我們知道獲取Attribute是通過反射來取的,Attribute那個值又是不變的,這樣就沒必要每次都要進行反射來獲取了?;谝陨蟽牲c代碼進行了如下的優(yōu)化,優(yōu)化后的代碼如下:
using System; using System.Collections.Concurrent; using System.Reflection; public static class CustomAttributeExtensions { /// <summary> /// Cache Data /// </summary> private static readonly ConcurrentDictionary<string, object> Cache = new ConcurrentDictionary<string, object>(); /// <summary> /// 獲取CustomAttribute Value /// </summary> /// <typeparam name="TAttribute">Attribute的子類型</typeparam> /// <typeparam name="TReturn">TReturn的子類型</typeparam> /// <param name="sourceType">頭部標有CustomAttribute類的類型</param> /// <param name="attributeValueAction">取Attribute具體哪個屬性值的匿名函數(shù)</param> /// <returns>返回Attribute的值,沒有則返回null</returns> public static TReturn GetCustomAttributeValue<TAttribute, TReturn>(this Type sourceType, Func<TAttribute, TReturn> attributeValueAction) where TAttribute : Attribute { return _getAttributeValue(sourceType, attributeValueAction, null); } /// <summary> /// 獲取CustomAttribute Value /// </summary> /// <typeparam name="TAttribute">Attribute的子類型</typeparam> /// <typeparam name="TReturn">TReturn的子類型</typeparam> /// <param name="sourceType">頭部標有CustomAttribute類的類型</param> /// <param name="attributeValueAction">取Attribute具體哪個屬性值的匿名函數(shù)</param> /// <param name="propertyName">field name或property name</param> /// <returns>返回Attribute的值,沒有則返回null</returns> public static TReturn GetCustomAttributeValue<TAttribute, TReturn>(this Type sourceType, Func<TAttribute, TReturn> attributeValueAction, string propertyName) where TAttribute : Attribute { return _getAttributeValue(sourceType, attributeValueAction, propertyName); } #region private methods private static TReturn _getAttributeValue<TAttribute, TReturn>(Type sourceType, Func<TAttribute, TReturn> attributeFunc, string propertyName) where TAttribute : Attribute { var cacheKey = BuildKey<TAttribute>(sourceType, propertyName); var value = Cache.GetOrAdd(cacheKey, k => GetValue(sourceType, attributeFunc, propertyName)); if (value is TReturn) return (TReturn)Cache[cacheKey]; return default(TReturn); } private static string BuildKey<TAttribute>(Type type, string propertyName) where TAttribute : Attribute { var attributeName = typeof(TAttribute).FullName; if (string.IsNullOrEmpty(propertyName)) { return type.FullName + "." + attributeName; } return type.FullName + "." + propertyName + "." + attributeName; } private static TReturn GetValue<TAttribute, TReturn>(this Type type, Func<TAttribute, TReturn> attributeValueAction, string name) where TAttribute : Attribute { TAttribute attribute = default(TAttribute); if (string.IsNullOrEmpty(name)) { attribute = type.GetCustomAttribute<TAttribute>(false); } else { var propertyInfo = type.GetProperty(name); if (propertyInfo != null) { attribute = propertyInfo.GetCustomAttribute<TAttribute>(false); } else { var fieldInfo = type.GetField(name); if (fieldInfo != null) { attribute = fieldInfo.GetCustomAttribute<TAttribute>(false); } } } return attribute == null ? default(TReturn) : attributeValueAction(attribute); } #endregion }
優(yōu)化后的代碼:
把不同的代碼用泛型T,Fun<TAttribute,TReturn>來處理來減少重復的代碼;
把取過的Attribute值存到一個ConcurrentDictionary中,下次再來取時,如果有則直接取ConcurrentDictionary中的值,如果沒有才通過反射來取相應的Attribute值,這樣大大的提高效率;
調用方法也更加的簡單了,代碼如下:
var customerInfoName = typeof(CustomerInfo).GetCustomAttributeValue<NameAttribute, string>(x => x.Name); var customerAddressName = typeof(CustomerInfo).GetCustomAttributeValue<NameAttribute, string>(x => x.Name, "Address"); var customerInfoDesc = typeof(CustomerInfo).GetCustomAttributeValue<DescriptionAttribute, string>(x => x.Description); Console.WriteLine("CustomerInfo Name:" + customerInfoName); Console.WriteLine("customerInfo >Address Name:" + customerAddressName); Console.WriteLine("customerInfo Desc:" + customerInfoDesc);
運行結果:
到此這篇關于C#自定義Attribute值的獲取與優(yōu)化的文章就介紹到這了,更多相關C# Attribute值獲取內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!