C# TypeInfo GetConstructor(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])

Description

TypeInfo GetConstructor(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[]) Searches for a constructor whose parameters match the specified argument types and modifiers, using the specified binding constraints and the specified calling convention.

Syntax

TypeInfo.GetConstructor(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[]) has the following syntax.


[ComVisibleAttribute(true)]/*  w  w w  .  j a v a 2  s.  c om*/
public ConstructorInfo GetConstructor(
  BindingFlags bindingAttr,
  Binder binder,
  CallingConventions callConvention,
  Type[] types,
  ParameterModifier[] modifiers
)

Parameters

TypeInfo.GetConstructor(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[]) has the following parameters.

  • bindingAttr - A bitmask comprised of one or more BindingFlags that specify how the search is conducted.
  • bindingAttr - -or-
  • bindingAttr - Zero, to return null.
  • binder - An object that defines a set of properties and enables binding, which can involve selection of an overloaded method, coercion of argument types, and invocation of a member through reflection.
  • binder - -or-
  • binder - A null reference (Nothing in Visual Basic), to use the DefaultBinder.
  • callConvention - The object that specifies the set of rules to use regarding the order and layout of arguments, how the return value is passed, what registers are used for arguments, and the stack is cleaned up.
  • types - An array of Type objects representing the number, order, and type of the parameters for the constructor to get.
  • types - -or-
  • types - An empty array of the type Type (that is, Type[] types = new Type[0]) to get a constructor that takes no parameters.
  • modifiers - An array of ParameterModifier objects representing the attributes associated with the corresponding element in the types array. The default binder does not process this parameter.

Returns

TypeInfo.GetConstructor(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[]) method returns An object representing the constructor that matches the specified requirements, if found; otherwise, null.

Example


using System;/* w w  w  .ja  v  a  2  s  .  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 »
    System.Reflection »




EventInfo
FieldInfo
MemberInfo
MethodInfo
ParameterInfo
TypeInfo