Contains Attribute by Field - CSharp System.Reflection

CSharp examples for System.Reflection:FieldInfo

Description

Contains Attribute by Field

Demo Code


using System.Reflection;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//from w w w  .  j a va 2s  . com

public class Main{

        public static T ContainsAttributebyField<T>(Type type, string FieldName) where T : Attribute
        {
            PropertyInfo[] piArr = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo pi in piArr)
            {
                if (pi.Name == FieldName)
                {
                    object[] arr = pi.GetCustomAttributes(typeof(T), true);
                    if (arr.Length == 0) return null;
                    return (T)arr[0];
                }
            }
            return null;
        }
}

Related Tutorials