在C#中,自定义特性的创建用于将声明信息与代码(程序集、类型、方法、属性等)相关联,以任何需要的方式。特性增强了.NET的可扩展性能。
1、创建特性步骤
1.1、定义一个自定义特性类,此特性类继承自System.Attribute类
1.2、自定义特性类的命名以Attribute为后缀
1.3、使用AttributeUsage特性来指定已创建的自定义特性类的使用范围
1.4、创建自定义特性的构造函数和可访问属性
public class MustInputValidAttribute : System.Attribute { /// <summary> /// 是否必输 /// </summary> public bool IsMustInput { get; set; } /// <summary> /// 提示信息 /// </summary> public string Message { get; set; } /// <summary> /// 构造函数 /// </summary> public MustInputValidAttribute() { } /// <summary> /// 构造函数 /// </summary> /// <param name="isMustInput">是否必输</param> /// <param name="message">提示信息</param> public MustInputValidAttribute(bool isMustInput, string message) { this.IsMustInput = isMustInput; this.Message = message; } }
2、读取特性代码
public class AttributeHelper { /// <summary> /// 获取成员变量对象中指定自定义特性名称的对象 /// </summary> /// <param name="field">成员对象</param> /// <param name="attributeName">自定义特性名称</param> /// <returns>自定义特性对象</returns> public static object GetCustomAttribute(FieldInfo field, string attributeName) { //参数检测 if (field == null) throw new ArgumentNullException("GetCustomAttribute.field"); if (string.IsNullOrEmpty(attributeName)) throw new ArgumentNullException("GetCustomAttribute.attributeName"); //获取属性对象所有自定义特性 object attribute = null; object[] attributes = field.GetCustomAttributes(true); if (attributes.Length <= 0) return attribute; foreach (object obj in attributes) { if (obj.GetType().Name == attributeName) { attribute = obj; break; } } return attribute; } /// <summary> /// 获取属性对象中指定自定义特性名称的对象 /// </summary> /// <param name="pi">属性对象</param> /// <param name="attributeName">自定义特性名称</param> /// <returns>自定义特性对象</returns> public static object GetCustomAttribute(PropertyInfo pi, string attributeName) { //参数检测 if (pi == null) throw new ArgumentNullException("GetCustomAttribute.pi"); if (string.IsNullOrEmpty(attributeName)) throw new ArgumentNullException("GetCustomAttribute.attributeName"); //获取属性对象所有自定义特性 object attribute = null; object[] attributes = pi.GetCustomAttributes(true); if (attributes.Length <= 0) return attribute; foreach (object obj in attributes) { if (obj.GetType().Name == attributeName) { attribute = obj; break; } } return attribute; } /// <summary> /// 获取方法对象中指定自定义特性名称的对象 /// </summary> /// <param name="pi">方法对象</param> /// <param name="attributeName">自定义特性名称</param> /// <returns>自定义特性对象</returns> public static object GetCustomAttribute(MethodInfo mi, string attributeName) { //参数检测 if (mi == null) throw new ArgumentNullException("GetCustomAttribute.mi"); if (string.IsNullOrEmpty(attributeName)) throw new ArgumentNullException("GetCustomAttribute.attributeName"); //获取属性对象所有自定义特性 object attribute = null; object[] attributes = mi.GetCustomAttributes(true); if (attributes.Length <= 0) return attribute; foreach (object obj in attributes) { if (obj.GetType().Name == attributeName) { attribute = obj; break; } } return attribute; } /// <summary> /// 获取方法对象中指定自定义特性名称的对象 /// </summary> /// <param name="pi">方法对象</param> /// <param name="attributeName">自定义特性名称</param> /// <returns>自定义特性对象</returns> public static object GetCustomAttribute(MethodBase mi, string attributeName) { //参数检测 if (mi == null) throw new ArgumentNullException("GetCustomAttribute.mi"); if (string.IsNullOrEmpty(attributeName)) throw new ArgumentNullException("GetCustomAttribute.attributeName"); //获取属性对象所有自定义特性 object attribute = null; object[] attributes = mi.GetCustomAttributes(true); if (attributes.Length <= 0) return attribute; foreach (object obj in attributes) { if (obj.GetType().Name == attributeName) { attribute = obj; break; } } return attribute; } /// <summary> /// 获取事件对象中指定自定义特性名称的对象 /// </summary> /// <param name="ei">事件对象</param> /// <param name="attributeName">自定义特性名称</param> /// <returns>自定义特性对象</returns> public static object GetCustomAttribute(EventInfo ei, string attributeName) { //参数检测 if (ei == null) throw new ArgumentNullException("GetCustomAttribute.ei"); if (string.IsNullOrEmpty(attributeName)) throw new ArgumentNullException("GetCustomAttribute.attributeName"); //获取属性对象所有自定义特性 object attribute = null; object[] attributes = ei.GetCustomAttributes(true); if (attributes.Length <= 0) return attribute; foreach (object obj in attributes) { if (obj.GetType().Name == attributeName) { attribute = obj; break; } } return attribute; } }
3、调用示例
attributeTemp = null; attributeTemp = AttributeHelper.GetCustomAttribute(pi, "MustInputValidAttribute");
相关阅读
文章评论