Programmatically Discover the Members of a Type - CSharp Reflection

CSharp examples for Reflection:Member

Description

Programmatically Discover the Members of a Type

Demo Code


using System;/*from   www  .j av  a2 s  . c  o  m*/
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

class MainClass
{
    static void Main(string[] args)
    {
        Type myType = typeof(MainClass);
        foreach (ConstructorInfo constr in myType.GetConstructors())
        {
            Console.Write("Constructor: ");
            foreach (ParameterInfo param in constr.GetParameters())
            {
                Console.Write("{0} ({1}), ", param.Name, param.ParameterType);
            }
            Console.WriteLine();
        }

        Console.WriteLine("\nMethods...");
        foreach (MethodInfo method in myType.GetMethods())
        {
            Console.Write(method.Name);
            // get the paramters for this constructor
            foreach (ParameterInfo param in method.GetParameters())
            {
                Console.Write("{0} ({1}), ", param.Name, param.ParameterType);
            }
            Console.WriteLine();
        }

        // get the property details
        Console.WriteLine("\nProperty...");
        foreach (PropertyInfo property in myType.GetProperties())
        {
            Console.Write("{0} ", property.Name);
            // get the paramters for this constructor
            foreach (MethodInfo accessor in property.GetAccessors())
            {
                Console.Write("{0}, ", accessor.Name);
            }
            Console.WriteLine();
        }

    }

    public string MyProperty
    {
        get;
        set;
    }

    public MainClass(string param1, int param2, char param3)
    {

    }
}

Result


Related Tutorials