Get the properties of the current Type, using the specified binding constraints in CSharp

Description

The following code shows how to get the properties of the current Type, using the specified binding constraints.

Example


using System;//ww  w  . j a v a2 s.co  m
using System.Reflection;
using System.Reflection.Emit;

public class MyTypeClass
{
    public String MyProperty1
    {
        get
        {
            return "hello";
        }
    }
    public String MyProperty2
    {
        get
        {
            return "hello";
        }
    }
    protected String MyProperty3
    {
        get
        {
            return "hello";
        }
    }
}

public class TypeMain
{
    public static void Main()
    {
        Type myType = (typeof(MyTypeClass));

        PropertyInfo[] myPropertyInfo = myType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
        Console.WriteLine("The number of public properties is {0}.", myPropertyInfo.Length);
   
        for (int i = 0; i < myPropertyInfo.Length; i++)
        {
            PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo[i];
            Console.WriteLine("The property name is {0}.", myPropInfo.Name);
            Console.WriteLine("The property type is {0}.", myPropInfo.PropertyType);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Reflection »




Array
Constructor
Event
Field
Interface
Method
Properties
Type