Get the ConstructorInfo object matching the specified binding flags, and displays the signature of the constructor. : Constructor « Reflection « C# / CSharp Tutorial






using System;
using System.Reflection;
using System.Security;


public class MyClass
{
    public MyClass(int i){}

    public static void Main()
    {
        try
        {
            Type myType = typeof(MyClass);
            Type[] types = new Type[1];
            types[0] = typeof(int);
            ConstructorInfo constructorInfoObj = myType.GetConstructor(BindingFlags.Instance | BindingFlags.Public, null, types, null);
            Console.WriteLine(constructorInfoObj.ToString());
        }
        catch(ArgumentNullException e)
        {
            Console.WriteLine("ArgumentNullException: " + e.Message);
        }
        catch(ArgumentException e)
        {
            Console.WriteLine("ArgumentException: " + e.Message);
        }
        catch(SecurityException e)
        {
            Console.WriteLine("SecurityException: " + e.Message);
        }
        catch(Exception e)
        {
            Console.WriteLine("Exception: " + e.Message);
        }
    }
}








19.3.Constructor
19.3.1.Get constructor information: Display return type, name and parameters
19.3.2.Find matching constructor
19.3.3.Dynamically invoking constructors
19.3.4.Utilize MyClass without assuming any prior knowledge: invoke constructor
19.3.5.Using a ConstructorInfo to create new instance
19.3.6.Call parent constructor from abstract class
19.3.7.Get the ConstructorInfo object matching the specified binding flags, and displays the signature of the constructor.