Get Attributes - CSharp System.Reflection

CSharp examples for System.Reflection:FieldInfo

Description

Get Attributes

Demo Code


using System.Text;
using System.Reflection;
using System.Linq.Expressions;
using System.Linq;
using System.Collections.Generic;
using System;/*from   w  w  w.ja  v  a 2s .c om*/

public class Main{
        public static T[] GetAttributes<T>(this ParameterInfo info, bool inherit) where T : Attribute
        {
            var os = info.GetCustomAttributes(typeof(T), inherit);
            return (T[])os;
        }
        public static T[] GetAttributes<T>(this Type info, bool inherit) where T : Attribute
        {
            var os = info.GetCustomAttributes(typeof(T), inherit);
            return (T[])os;
        }
        public static T[] GetAttributes<T>(this PropertyInfo info, bool inherit) where T : Attribute
        {
            var os = info.GetCustomAttributes(typeof(T), inherit);
            return (T[])os;
        }
        public static T[] GetAttributes<T>(this FieldInfo info, bool inherit) where T : Attribute
        {
            var os = info.GetCustomAttributes(typeof(T), inherit);
            return (T[])os;
        }
        public static T[] GetAttributes<T>(this MethodInfo info, bool inherit) where T : Attribute
        {
            var os = info.GetCustomAttributes(typeof(T), inherit);
            return (T[])os;
        }
}

Related Tutorials