C#自定义Attribute值的获取与优化技巧

Dabria ·
更新时间:2024-09-20
· 373 次阅读

目录

首先说下什么是Atrribute

Attribute作为编译器的指令

C# Attribute值获取

首先说下什么是Atrribute

首先,我们肯定Attribute是一个类,下面是msdn文档对它的描述:

公共语言运行时允许你添加类似关键字的描述声明,叫做attributes,它对程序中的元素进行标注,如类型、字段、方法和属性等。Attributes和Microsoft .NET Framework文件的元数据保存在一起,可以用来向运行时描述你的代码,或者在程序运行的时候影响应用程序的行为。

在.NET中,Attribute被用来处理多种问题,比如序列化、程序的安全特征、防止即时编译器对程序代码进行优化从而代码容易调试等等。下面,我们先来看几个在.NET中标准的属性的使用,稍后我们再回过头来讨论Attribute这个类本身。(文中的代码使用C#编写,但同样适用所有基于.NET的所有语言)

Attribute作为编译器的指令

在C#中存在着一定数量的编译器指令,如:#define DEBUG, #undefine DEBUG, #if等。这些指令专属于C#,而且在数量上是固定的。而Attribute用作编译器指令则不受数量限制。比如下面的三个Attribute:

   Conditional:起条件编译的作用,只有满足条件,才允许编译器对它的代码进行编译。一般在程序调试的时候使用。

   DllImport:用来标记非.NET的函数,表明该方法在一个外部的DLL中定义。

   Obsolete:这个属性用来标记当前的方法已经被废弃,不再使用了。

C# Attribute值获取

C#自定义Attribute值的获取是开发中会经常用到的,一般我们的做法也就是用反射进行获取的,代码也不是很复杂。

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值了。虽然我们是获取到了我们想要的,但是我们发现这样做是不是太累了,如果又扩展一个自定义的Attribute,或者又在一个新的属性或字段上标上Attribute时,我们又要写一段代码来实现我想要的,这些严重代码违反了DRY的设计原则。我们知道获取Attribute是通过反射来取的,Attribute那个值又是不变的,这样就没必要每次都要进行反射来获取了。基于以上两点代码进行了如下的优化,优化后的代码如下:

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具体哪个属性值的匿名函数</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具体哪个属性值的匿名函数</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 }

优化后的代码:

把不同的代码用泛型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值的获取与优化的文章就介绍到这了,更多相关C# Attribute值获取内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



C# 技巧 优化

需要 登录 后方可回复, 如果你还没有账号请 注册新账号