Check if a method is a constructor in CSharp

Description

The following code shows how to check if a method is a constructor.

Example


using System;// w w  w .  jav a  2s. c o m
using System.Reflection;

public class Example
{
    public void m_public() {}
    internal void m_internal() {}
    protected void m_protected() {}
    protected internal void m_protected_public() {}

    public static void Main()
    {
        foreach (MethodBase m in typeof(Example).GetMethods(
            BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
        {
            if (m.Name.Substring(0, 1) == "m")
            {
                Console.WriteLine(m.Name);
                Console.WriteLine(m.IsConstructor);
            }
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Reflection »




Array
Constructor
Event
Field
Interface
Method
Properties
Type