#region COPYRIGHT
//
//     THIS IS GENERATED BY TEMPLATE
//     
//     AUTHOR  :     ROYE
//     DATE       :     2010
//
//     COPYRIGHT (C) 2010, TIANXIAHOTEL TECHNOLOGIES CO., LTD. ALL RIGHTS RESERVED.
//
#endregion

using System;
using System.Collections;
using System.Text;

namespace System.Reflection
{
    /// <summary>
    /// 提供一组对类型公用方法
    /// </summary>
    public static class ClassHelper
    {
        #region GetAttribute<T>

        public static T GetAttribute<T>(Type type, bool inherit) where T : Attribute
        {
            T[] types = GetAttributes<T>(type, inherit);
            if (types != null && types.Length > 0)
            {
                return types[0];
            }
            else
            {
                return null;
            }
        }

        public static T GetAttribute<T>(ConstructorInfo ci, bool inherit) where T : Attribute
        {
            T[] types = GetAttributes<T>(ci, inherit);
            if (types != null && types.Length > 0)
            {
                return types[0];
            }
            else
            {
                return null;
            }
        }

        public static T GetAttribute<T>(FieldInfo fi, bool inherit) where T : Attribute
        {
            T[] types = GetAttributes<T>(fi, inherit);
            if (types != null && types.Length > 0)
            {
                return types[0];
            }
            else
            {
                return null;
            }
        }

        public static T GetAttribute<T>(MethodInfo mi, bool inherit) where T : Attribute
        {
            T[] types = GetAttributes<T>(mi, inherit);
            if (types != null && types.Length > 0)
            {
                return types[0];
            }
            else
            {
                return null;
            }
        }

        public static T GetAttribute<T>(PropertyInfo pi, bool inherit) where T : Attribute
        {
            T[] types = GetAttributes<T>(pi, inherit);
            if (types != null && types.Length > 0)
            {
                return types[0];
            }
            else
            {
                return null;
            }
        }

        #endregion

        #region GetAttributes<T>

        public static T[] GetAttributes<T>(Type type, bool inherit) where T : Attribute
        {
            object[] objects = type.GetCustomAttributes(typeof(T), inherit);
            return (T[])objects;
        }

        public static T[] GetAttributes<T>(ConstructorInfo ci, bool inherit) where T : Attribute
        {
            object[] objects = ci.GetCustomAttributes(typeof(T), inherit);
            return (T[])objects;
        }

        public static T[] GetAttributes<T>(FieldInfo fi, bool inherit) where T : Attribute
        {
            object[] objects = fi.GetCustomAttributes(typeof(T), inherit);
            return (T[])objects;
        }

        public static T[] GetAttributes<T>(MethodInfo mi, bool inherit) where T : Attribute
        {
            object[] objects = mi.GetCustomAttributes(typeof(T), inherit);
            return (T[])objects;
        }

        public static T[] GetAttributes<T>(PropertyInfo pi, bool inherit) where T : Attribute
        {
            object[] objects = pi.GetCustomAttributes(typeof(T), inherit);
            return (T[])objects;
        }

        #endregion

        #region HasAttribute<T>

        public static bool HasAttribute<T>(Type type, bool inherit, out T attribute) where T : Attribute
        {
            return (attribute = GetAttribute<T>(type, inherit)) != null;
        }

        public static bool HasAttribute<T>(Type type, bool inherit) where T : Attribute
        {
            return GetAttribute<T>(type, inherit) != null;
        }

        public static bool HasAttribute<T>(ConstructorInfo ci, bool inherit, out T attribute) where T : Attribute
        {
            return (attribute = GetAttribute<T>(ci, inherit)) != null;
        }

        public static bool HasAttribute<T>(ConstructorInfo ci, bool inherit) where T : Attribute
        {
            return GetAttribute<T>(ci, inherit) != null;
        }

        public static bool HasAttribute<T>(FieldInfo info, bool inherit, out T attribute) where T : Attribute
        {
            return (attribute = GetAttribute<T>(info, inherit)) != null;
        }

        public static bool HasAttribute<T>(FieldInfo info, bool inherit) where T : Attribute
        {
            return GetAttribute<T>(info, inherit) != null;
        }

        public static bool HasAttribute<T>(MethodInfo info, bool inherit, out T attribute) where T : Attribute
        {
            return (attribute = GetAttribute<T>(info, inherit)) != null;
        }

        public static bool HasAttribute<T>(MethodInfo info, bool inherit) where T : Attribute
        {
            return GetAttribute<T>(info, inherit) != null;
        }

        public static bool HasAttribute<T>(PropertyInfo info, bool inherit, out T attribute) where T : Attribute
        {
            return (attribute = GetAttribute<T>(info, inherit)) != null;
        }

        public static bool HasAttribute<T>(PropertyInfo info, bool inherit) where T : Attribute
        {
            return GetAttribute<T>(info, inherit) != null;
        }

        #endregion
    }
}
