Search Property Info From Attribute - CSharp System.Reflection

CSharp examples for System.Reflection:PropertyInfo

Description

Search Property Info From Attribute

Demo Code


using System.Reflection;
using System.Linq;
using System;/*from ww  w  . ja v  a2s.  c  o  m*/

public class Main{
        public static PropertyInfo SearchPropertyInfoFromAttribute<TAttribute>(Type type) where TAttribute:Attribute 
        {
            foreach (var propertyInfo in type.GetProperties())
            {
                object[] attributes = propertyInfo.GetCustomAttributes(typeof (TAttribute), true);
                if (attributes.Length == 1)
                    return propertyInfo;
            }
            return null;
        }
}

Related Tutorials