通过反射访问属性(Attribute)信息的工具类

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;


    /// <summary>
    /// 通过反射访问属性(Attribute)信息的工具类
    /// </summary>
    public static class AttributeHelper
    {
        /// <summary>
        /// 获取某个类型包括指定属性的集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="type"></param>
        /// <returns></returns>
        public static IList<T> GetCustomAttributes<T>(Type type) where T : Attribute
        {
            if (type == null) throw new ArgumentNullException("type");
            T[] attributes = (T[])(type.GetCustomAttributes(typeof(T), false));
            return (attributes.Length == 0) ? null : new List<T>(attributes);
        }

        /// <summary>
        /// 获得某各类型包括指定属性的所有方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="type"></param>
        /// <returns></returns>
        public static IList<MethodInfo> GetMethodsWithCustomAttribute<T>(Type type) where T : Attribute
        {
            if (type == null) throw new ArgumentNullException("type");
            MethodInfo[] methods = type.GetMethods();
            if ((methods == null) || (methods.Length == 0)) return null;
            IList<MethodInfo> result = new List<MethodInfo>();
            foreach (MethodInfo method in methods)
                if (method.IsDefined(typeof(T), false))
                    result.Add(method);
            return result.Count == 0 ? null : result;
        }

        /// <summary>
        /// 获取某个方法指定类型属性的集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="method"></param>
        /// <returns></returns>
        public static IList<T> GetMethodCustomAttributes<T>(MethodInfo method) where T : Attribute
        {
            if (method == null) throw new ArgumentNullException("method");
            T[] attributes = (T[])(method.GetCustomAttributes(typeof(T), false));
            return (attributes.Length == 0) ? null : new List<T>(attributes);
        }
      
        /// <summary>
        /// 获取某个方法指定类型的属性
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="method"></param>
        /// <returns></returns>
        public static T GetMethodCustomAttribute<T>(MethodInfo method) where T : Attribute
        {
            IList<T> attributes = GetMethodCustomAttributes<T>(method);
            return (attributes == null) ? null : attributes[0];
        }

        /// <summary>
        /// 获得某各类型包括指定属性的所有属性
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="type"></param>
        /// <returns></returns>
        public static IList<PropertyInfo> GetPropertyWithCustomAttribute<T>(Type type) where T : Attribute
        {
            if (type == null) throw new ArgumentNullException("type");
            PropertyInfo[] propes = type.GetProperties();
            if ((propes == null) || (propes.Length == 0)) return null;
            IList<PropertyInfo> result = new List<PropertyInfo>();
            foreach (PropertyInfo p in propes)
                if (p.IsDefined(typeof(T), false))
                    result.Add(p);
            return result.Count == 0 ? null : result;
        }

        /// <summary>
        /// 获取某个属性指定类型属性的集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="method"></param>
        /// <returns></returns>
        public static IList<T> GetPropertyCustomAttributes<T>(PropertyInfo property) where T : Attribute
        {
            if (property == null) throw new ArgumentNullException("method");
            T[] attributes = (T[])(property.GetCustomAttributes(typeof(T), false));
            return (attributes.Length == 0) ? null : new List<T>(attributes);
        }
        /// <summary>
        /// 获取某个属性指定类型的属性
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="method"></param>
        /// <returns></returns>
        public static T GetPropertyCustomAttribute<T>(PropertyInfo property) where T : Attribute
        {
            IList<T> attributes = GetPropertyCustomAttributes<T>(property);
            return (attributes == null) ? null : attributes[0];
        }
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值