コード例 #1
0
 /// <summary>
 /// 从类型成员获取指定Attribute特性
 /// </summary>
 /// <typeparam name="T">Attribute特性类型</typeparam>
 /// <param name="memberInfo">类型类型成员</param>
 /// <param name="inherit">是否从继承中查找</param>
 /// <returns>返回所有指定Attribute特性的数组</returns>
 public static T[] GetAttributes <T>(this MemberInfo memberInfo, bool inherit = true) where T : Attribute
 {
     return(memberInfo.GetCustomAttributes(typeof(T), inherit).Cast <T>().ToArray());
 }
コード例 #2
0
        /// <summary>
        /// 从类型成员获取指定Attribute特性
        /// </summary>
        /// <typeparam name="T">Attribute特性类型</typeparam>
        /// <param name="memberInfo">类型类型成员</param>
        /// <param name="inherit">是否从继承中查找</param>
        /// <returns>存在返回第一个,不存在返回null</returns>
        public static T GetAttribute <T>(this MemberInfo memberInfo, bool inherit = true) where T : Attribute
        {
            var attributes = memberInfo.GetCustomAttributes(typeof(T), inherit);

            return(attributes.FirstOrDefault() as T);
        }