Get properties defined by the current type in CSharp

Description

The following code shows how to get properties defined by the current type.

Example


using System;/*from   ww w  .  ja va 2  s.  c  o m*/
using System.Collections.Generic;
using System.Reflection;
using System.Globalization;
using System.Text;

public class MainClass
{
    public static void Main(String[] argv){    
        TypeInfo t = typeof(Calendar).GetTypeInfo();
        IEnumerable<PropertyInfo> pList = t.DeclaredProperties;
        IEnumerable<MethodInfo> mList = t.DeclaredMethods;

        StringBuilder sb = new StringBuilder();

        sb.Append("Properties:");
        foreach (PropertyInfo p in pList)
        {

            sb.Append("\n" + p.DeclaringType.Name + ": " + p.Name);
        }
        sb.Append("\nMethods:");
        foreach (MethodInfo m in mList)
        {
            sb.Append("\n" + m.DeclaringType.Name + ": " + m.Name);
        }

        Console.WriteLine(sb.ToString());

    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Reflection »




Array
Constructor
Event
Field
Interface
Method
Properties
Type