Get Property from Object - CSharp System.Reflection

CSharp examples for System.Reflection:PropertyInfo

Description

Get Property from Object

Demo Code


using System.Text;
using System.Reflection;
using System;/*from w  ww .  j  a v a2 s  .  c o  m*/

public class Main{
        public static object GetProperty(this object obj, string name)
        {
            var propertyInfo = obj.GetType().GetProperty(name,
                BindingFlags.GetProperty |
                BindingFlags.IgnoreCase |
                BindingFlags.Public |
                BindingFlags.NonPublic |
                BindingFlags.Instance);
            return propertyInfo == null ? null : propertyInfo.GetValue(obj, null);
        }
}

Related Tutorials