Get Property Info - CSharp System.Reflection

CSharp examples for System.Reflection:PropertyInfo

Description

Get Property Info

Demo Code


using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Reflection;
using System.ComponentModel;
using System.Collections.Generic;
using System;/* ww w  .  j a v  a  2  s.c o  m*/

public class Main{
        private static PropertyInfo GetPropertyInfo(object instance, string propertyName) {
            if (instance == null) {
                throw new ArgumentNullException("instance");
            }
            if (String.IsNullOrEmpty(propertyName)) {
                throw new ArgumentException("A property must be specified.", "propertyName");
            }
            PropertyInfo propInfo = instance.GetType().GetProperty(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            if (propInfo == null) {
                throw new ArgumentException(String.Format(
                    "A property named '{0}' on type '{1}' could not be found.",
                    propertyName, instance.GetType().FullName));
            }
            return propInfo;
        }
}

Related Tutorials