Find Property in Type - CSharp System.Reflection

CSharp examples for System.Reflection:PropertyInfo

Description

Find Property in Type

Demo Code


using System.Reflection;
using System.Linq;
using System.Collections.Generic;
using System;/*from  w  ww  .j a v  a  2 s  .c o  m*/

public class Main{

        public static PropertyInfo FindProperty(this Type type, string propertyName, BindingFlags bindingAttr = BindingFlags.Instance | BindingFlags.NonPublic)
        {
            if (!string.IsNullOrWhiteSpace(propertyName))
            {
                var property = type.GetProperty(propertyName, bindingAttr);
                if (property != null)
                {
                    return property;
                }
                else if (type.BaseType != null)
                {
                    return type.BaseType.FindProperty(propertyName, bindingAttr);
                }
            }
            return null;
        }
}

Related Tutorials