Get Attribute - CSharp System.Reflection

CSharp examples for System.Reflection:PropertyInfo

Description

Get Attribute

Demo Code

//     COPYRIGHT (C) 2010, TIANXIAHOTEL TECHNOLOGIES CO., LTD. ALL RIGHTS RESERVED.
using System.Text;
using System.Collections;
using System;/*from   w w  w.j a  v  a2 s .  com*/

public class Main{
        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;
            }
        }
        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>(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>(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;
            }
        }
        #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[] GetAttributes<T>(PropertyInfo pi, bool inherit) where T : Attribute
        {
            object[] objects = pi.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>(FieldInfo fi, bool inherit) where T : Attribute
        {
            object[] objects = fi.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>(Type type, bool inherit) where T : Attribute
        {
            object[] objects = type.GetCustomAttributes(typeof(T), inherit);
            return (T[])objects;
        }
}

Related Tutorials