Get a constructor by parameters and modifiers using binding constraints and calling convention in CSharp

Description

The following code shows how to get a constructor by parameters and modifiers using binding constraints and calling convention.

Example


using System;/*  w ww  .jav a 2s  . c om*/
using System.Reflection;
using System.Security;

public class MyClass1
{
    public MyClass1(int i){}
    public static void Main()
    {
       Type  myType = typeof(MyClass1);
       Type[] types = new Type[1];
       types[0] = typeof(int);
       ConstructorInfo constructorInfoObj = myType.GetConstructor(
           BindingFlags.Instance | BindingFlags.Public, null,
           CallingConventions.HasThis, types, null);
       if(constructorInfoObj != null)
       {
           Console.WriteLine("The constructor of MyClass1 that is a public " +
               "instance method and takes an integer as a parameter is: ");
           Console.WriteLine(constructorInfoObj.ToString());
       }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Reflection »




Array
Constructor
Event
Field
Interface
Method
Properties
Type