Type.GetConstructor Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets a specific constructor of the current Type.
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. |
GetConstructor(BindingFlags, Binder, Type[], ParameterModifier[]) |
Searches for a constructor whose parameters match the specified argument types and modifiers, using the specified binding constraints. |
GetConstructor(BindingFlags, Type[]) |
Searches for a constructor whose parameters match the specified argument types, using the specified binding constraints. |
GetConstructor(Type[]) |
Searches for a public instance constructor whose parameters match the types in the specified array. |
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
Searches for a constructor whose parameters match the specified argument types and modifiers, using the specified binding constraints and the specified calling convention.
public:
System::Reflection::ConstructorInfo ^ GetConstructor(System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, System::Reflection::CallingConventions callConvention, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public:
virtual System::Reflection::ConstructorInfo ^ GetConstructor(System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, System::Reflection::CallingConventions callConvention, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public System.Reflection.ConstructorInfo? GetConstructor (System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, System.Reflection.CallingConventions callConvention, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.ConstructorInfo GetConstructor (System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Reflection.CallingConventions callConvention, Type[] types, System.Reflection.ParameterModifier[] modifiers);
[System.Runtime.InteropServices.ComVisible(true)]
public System.Reflection.ConstructorInfo GetConstructor (System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Reflection.CallingConventions callConvention, Type[] types, System.Reflection.ParameterModifier[] modifiers);
member this.GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.ConstructorInfo
abstract member GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.ConstructorInfo
override this.GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.ConstructorInfo
[<System.Runtime.InteropServices.ComVisible(true)>]
abstract member GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.ConstructorInfo
override this.GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.ConstructorInfo
Public Function GetConstructor (bindingAttr As BindingFlags, binder As Binder, callConvention As CallingConventions, types As Type(), modifiers As ParameterModifier()) As ConstructorInfo
Parameters
- bindingAttr
- BindingFlags
A bitwise combination of the enumeration values that specify how the search is conducted.
-or-
Default to return null
.
- binder
- 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.
-or-
A null reference (Nothing
in Visual Basic), to use the DefaultBinder.
- callConvention
- CallingConventions
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
- Type[]
An array of Type objects representing the number, order, and type of the parameters for the constructor to get.
-or-
An empty array of the type Type (that is, Type[] types = new Type[0]) to get a constructor that takes no parameters.
- modifiers
- ParameterModifier[]
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
An object representing the constructor that matches the specified requirements, if found; otherwise, null
.
Implements
- Attributes
Exceptions
types
is multidimensional.
-or-
modifiers
is multidimensional.
-or-
types
and modifiers
do not have the same length.
Examples
The following example obtains the type of MyClass
, gets the ConstructorInfo object, and displays the constructor signature.
using namespace System;
using namespace System::Reflection;
using namespace System::Security;
public ref class MyClass1
{
public:
MyClass1( int i ){}
};
int main()
{
try
{
Type^ myType = MyClass1::typeid;
array<Type^>^types = gcnew array<Type^>(1);
types[ 0 ] = int::typeid;
// Get the public instance constructor that takes an integer parameter.
ConstructorInfo^ constructorInfoObj = myType->GetConstructor( static_cast<BindingFlags>(BindingFlags::Instance | BindingFlags::Public), nullptr, CallingConventions::HasThis, types, nullptr );
if ( constructorInfoObj != nullptr )
{
Console::WriteLine( "The constructor of MyClass1 that is a public instance method and takes an integer as a parameter is: " );
Console::WriteLine( constructorInfoObj );
}
else
{
Console::WriteLine( "The constructor of MyClass1 that is a public instance method and takes an integer as a parameter is not available." );
}
}
catch ( ArgumentNullException^ e )
{
Console::WriteLine( "ArgumentNullException: {0}", e->Message );
}
catch ( ArgumentException^ e )
{
Console::WriteLine( "ArgumentException: {0}", e->Message );
}
catch ( SecurityException^ e )
{
Console::WriteLine( "SecurityException: {0}", e->Message );
}
catch ( Exception^ e )
{
Console::WriteLine( "Exception: {0}", e->Message );
}
}
using System;
using System.Reflection;
using System.Security;
public class MyClass3
{
public MyClass3(int i) { }
public static void Main()
{
try
{
Type myType = typeof(MyClass3);
Type[] types = new Type[1];
types[0] = typeof(int);
// Get the public instance constructor that takes an integer parameter.
ConstructorInfo constructorInfoObj = myType.GetConstructor(
BindingFlags.Instance | BindingFlags.Public, null,
CallingConventions.HasThis, types, null);
if (constructorInfoObj != null)
{
Console.WriteLine("The constructor of MyClass3 that is a public " +
"instance method and takes an integer as a parameter is: ");
Console.WriteLine(constructorInfoObj.ToString());
}
else
{
Console.WriteLine("The constructor of MyClass3 that is a public instance " +
"method and takes an integer as a parameter is not available.");
}
}
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);
}
}
}
open System
open System.Reflection
open System.Security
type MyClass1(i: int) = class end
try
let myType = typeof<MyClass1>
let types = [| typeof<int> |]
// Get the public instance constructor that takes an integer parameter.
let constructorInfoObj = myType.GetConstructor(BindingFlags.Instance ||| BindingFlags.Public, null, CallingConventions.HasThis, types, null)
if constructorInfoObj <> null then
printfn "The constructor of MyClass1 that is a public instance method and takes an integer as a parameter is: \n{constructorInfoObj}"
else
printfn "The constructor of MyClass1 that is a public instance method and takes an integer as a parameter is not available."
with
| :? ArgumentNullException as e ->
printfn $"ArgumentNullException: {e.Message}"
| :? ArgumentException as e ->
printfn $"ArgumentException: {e.Message}"
| :? SecurityException as e ->
printfn $"SecurityException: {e.Message}"
| e ->
printfn $"Exception: {e.Message}"
Public Class MyClass1
Public Sub New(ByVal i As Integer)
End Sub
Public Shared Sub Main()
Try
Dim myType As Type = GetType(MyClass1)
Dim types(0) As Type
types(0) = GetType(Integer)
' Get the public instance constructor that takes an integer parameter.
Dim constructorInfoObj As ConstructorInfo = _
myType.GetConstructor(BindingFlags.Instance Or _
BindingFlags.Public, Nothing, _
CallingConventions.HasThis, types, Nothing)
If Not (constructorInfoObj Is Nothing) Then
Console.WriteLine("The constructor of MyClass1 that " + _
"is a public instance method and takes an " + _
"integer as a parameter is: ")
Console.WriteLine(constructorInfoObj.ToString())
Else
Console.WriteLine("The constructor MyClass1 that " + _
"is a public instance method and takes an " + _
"integer as a parameter is not available.")
End If
Catch e As ArgumentNullException
Console.WriteLine("ArgumentNullException: " + e.Message)
Catch e As ArgumentException
Console.WriteLine("ArgumentException: " + e.Message)
Catch e As SecurityException
Console.WriteLine("SecurityException: " + e.Message)
Catch e As Exception
Console.WriteLine("Exception: " + e.Message)
End Try
End Sub
End Class
Remarks
Although the default binder does not process ParameterModifier (the modifiers
parameter), you can use the abstract System.Reflection.Binder class to write a custom binder that does process modifiers
. ParameterModifier
is only used when calling through COM interop, and only parameters that are passed by reference are handled.
If an exact match does not exist, the binder
will attempt to coerce the parameter types specified in the types
array in order to select a match. If the binder
is unable to select a match, then null
is returned.
The following BindingFlags filter flags can be used to define which constructors to include in the search:
You must specify either
BindingFlags.Instance
orBindingFlags.Static
in order to get a return.Specify
BindingFlags.Public
to include public constructors in the search.Specify
BindingFlags.NonPublic
to include non-public constructors (that is, private, internal, and protected constructors) in the search.
See System.Reflection.BindingFlags for more information.
To get the class initializer (static constructor) using this method, you must specify BindingFlags.Static | BindingFlags.NonPublic (BindingFlags.StaticOr
BindingFlags.NonPublic in Visual Basic). You can also get the class initializer using the TypeInitializer property.
The following table shows what members of a base class are returned by the Get
methods when reflecting on a type.
Member Type | Static | Non-Static |
---|---|---|
Constructor | No | No |
Field | No | Yes. A field is always hide-by-name-and-signature. |
Event | Not applicable | The common type system rule is that the inheritance is the same as that of the methods that implement the property. Reflection treats properties as hide-by-name-and-signature. See note 2 below. |
Method | No | Yes. A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature. |
Nested Type | No | No |
Property | Not applicable | The common type system rule is that the inheritance is the same as that of the methods that implement the property. Reflection treats properties as hide-by-name-and-signature. See note 2 below. |
Hide-by-name-and-signature considers all of the parts of the signature, including custom modifiers, return types, parameter types, sentinels, and unmanaged calling conventions. This is a binary comparison.
For reflection, properties and events are hide-by-name-and-signature. If you have a property with both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and you will not be able to access the setter on the base class.
Custom attributes are not part of the common type system.
Note
You cannot omit parameters when looking up constructors and methods. You can only omit parameters when invoking.
If the current Type represents a constructed generic type, this method returns the ConstructorInfo with the type parameters replaced by the appropriate type arguments. If the current Type represents a type parameter in the definition of a generic type or generic method, this method always returns null
.
See also
- ConstructorInfo
- BindingFlags
- Binder
- DefaultBinder
- CallingConventions
- ParameterModifier
- GetConstructorImpl(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetConstructors()
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
Searches for a constructor whose parameters match the specified argument types and modifiers, using the specified binding constraints.
public:
System::Reflection::ConstructorInfo ^ GetConstructor(System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public:
virtual System::Reflection::ConstructorInfo ^ GetConstructor(System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public System.Reflection.ConstructorInfo? GetConstructor (System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.ConstructorInfo GetConstructor (System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, Type[] types, System.Reflection.ParameterModifier[] modifiers);
[System.Runtime.InteropServices.ComVisible(true)]
public System.Reflection.ConstructorInfo GetConstructor (System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, Type[] types, System.Reflection.ParameterModifier[] modifiers);
member this.GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.ConstructorInfo
abstract member GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.ConstructorInfo
override this.GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.ConstructorInfo
[<System.Runtime.InteropServices.ComVisible(true)>]
abstract member GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.ConstructorInfo
override this.GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.ConstructorInfo
Public Function GetConstructor (bindingAttr As BindingFlags, binder As Binder, types As Type(), modifiers As ParameterModifier()) As ConstructorInfo
Parameters
- bindingAttr
- BindingFlags
A bitwise combination of the enumeration values that specify how the search is conducted.
-or-
Default to return null
.
- binder
- 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.
-or-
A null reference (Nothing
in Visual Basic), to use the DefaultBinder.
- types
- Type[]
An array of Type objects representing the number, order, and type of the parameters for the constructor to get.
-or-
An empty array of the type Type (that is, Type[] types = new Type[0]) to get a constructor that takes no parameters.
-or-
- modifiers
- ParameterModifier[]
An array of ParameterModifier objects representing the attributes associated with the corresponding element in the parameter type array. The default binder does not process this parameter.
Returns
A ConstructorInfo object representing the constructor that matches the specified requirements, if found; otherwise, null
.
Implements
- Attributes
Exceptions
types
is multidimensional.
-or-
modifiers
is multidimensional.
-or-
types
and modifiers
do not have the same length.
Examples
The following example obtains the type of MyClass
, gets the ConstructorInfo object, and displays the constructor signature.
using namespace System;
using namespace System::Reflection;
using namespace System::Security;
public ref class MyClass1
{
public:
MyClass1( int i ){}
};
int main()
{
try
{
Type^ myType = MyClass1::typeid;
array<Type^>^types = gcnew array<Type^>(1);
types[ 0 ] = int::typeid;
// Get the constructor that is public and takes an integer parameter.
ConstructorInfo^ constructorInfoObj = myType->GetConstructor( static_cast<BindingFlags>(BindingFlags::Instance | BindingFlags::Public), nullptr, types, nullptr );
if ( constructorInfoObj != nullptr )
{
Console::WriteLine( "The constructor of MyClass1 that is public and takes an integer as a parameter is:" );
Console::WriteLine( constructorInfoObj );
}
else
{
Console::WriteLine( "The constructor of the MyClass1 that is public and takes an integer as a parameter is not available." );
}
}
catch ( ArgumentNullException^ e )
{
Console::WriteLine( "ArgumentNullException: {0}", e->Message );
}
catch ( ArgumentException^ e )
{
Console::WriteLine( "ArgumentException: {0}", e->Message );
}
catch ( SecurityException^ e )
{
Console::WriteLine( "SecurityException: {0}", e->Message );
}
catch ( Exception^ e )
{
Console::WriteLine( "Exception: {0}", e->Message );
}
}
using System;
using System.Reflection;
using System.Security;
public class MyClass2
{
public MyClass2(int i) { }
public static void Main()
{
try
{
Type myType = typeof(MyClass2);
Type[] types = new Type[1];
types[0] = typeof(int);
// Get the constructor that is public and takes an integer parameter.
ConstructorInfo constructorInfoObj = myType.GetConstructor(
BindingFlags.Instance | BindingFlags.Public, null, types, null);
if (constructorInfoObj != null)
{
Console.WriteLine("The constructor of MyClass2 that is public " +
"and takes an integer as a parameter is:");
Console.WriteLine(constructorInfoObj.ToString());
}
else
{
Console.WriteLine("The constructor of the MyClass2 that is public " +
"and takes an integer as a parameter is not available.");
}
}
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);
}
}
}
open System
open System.Reflection
open System.Security
type MyClass1(i: int) = class end
try
let myType = typeof<MyClass1>
let types = [| typeof<int> |]
// Get the constructor that is public and takes an integer parameter.
let constructorInfoObj = myType.GetConstructor(BindingFlags.Instance ||| BindingFlags.Public, null, types, null)
if constructorInfoObj <> null then
printfn "The constructor of MyClass1 that is public and takes an integer as a parameter is:\n{constructorInfoObj}"
else
printfn "The constructor of the MyClass1 that is public and takes an integer as a parameter is not available."
with
| :? ArgumentNullException as e ->
printfn $"ArgumentNullException: {e.Message}"
| :? ArgumentException as e ->
printfn $"ArgumentException: {e.Message}"
| :? SecurityException as e ->
printfn $"SecurityException: {e.Message}"
| e ->
printfn $"Exception: {e.Message}"
Imports System.Reflection
Imports System.Security
Public Class MyClass1
Public Sub New(ByVal i As Integer)
End Sub
Public Shared Sub Main()
Try
Dim myType As Type = GetType(MyClass1)
Dim types(0) As Type
types(0) = GetType(Integer)
' Get the constructor that is public and takes an integer parameter.
Dim constructorInfoObj As ConstructorInfo = _
myType.GetConstructor(BindingFlags.Instance Or _
BindingFlags.Public, Nothing, types, Nothing)
If Not (constructorInfoObj Is Nothing) Then
Console.WriteLine("The constructor of MyClass1 that is " + _
"public and takes an integer as a parameter is ")
Console.WriteLine(constructorInfoObj.ToString())
Else
Console.WriteLine("The constructor of MyClass1 that is " + _
"public and takes an integer as a parameter is not available.")
End If
Catch e As ArgumentNullException
Console.WriteLine("ArgumentNullException: " + e.Message)
Catch e As ArgumentException
Console.WriteLine("ArgumentException: " + e.Message)
Catch e As SecurityException
Console.WriteLine("SecurityException: " + e.Message)
Catch e As Exception
Console.WriteLine("Exception: " + e.Message)
End Try
End Sub
End Class
Remarks
If an exact match does not exist, the binder
will attempt to coerce the parameter types specified in the types
array in order to select a match. If the binder
is unable to select a match, then null
is returned.
The following BindingFlags filter flags can be used to define which constructors to include in the search:
You must specify either
BindingFlags.Instance
orBindingFlags.Static
in order to get a return.Specify
BindingFlags.Public
to include public constructors in the search.Specify
BindingFlags.NonPublic
to include non-public constructors (that is, private, internal, and protected constructors) in the search.
See System.Reflection.BindingFlags for more information.
To get the class initializer (static constructor) using this method overload, you must specify BindingFlags.Static | BindingFlags.NonPublic (BindingFlags.StaticOr
BindingFlags.NonPublic in Visual Basic). You can also get the class initializer using the TypeInitializer property.
Note
You cannot omit parameters when looking up constructors and methods. You can only omit parameters when invoking.
If the current Type represents a constructed generic type, this method returns the ConstructorInfo with the type parameters replaced by the appropriate type arguments. If the current Type represents a type parameter in the definition of a generic type or generic method, this method always returns null
.
See also
- ConstructorInfo
- BindingFlags
- Binder
- DefaultBinder
- ParameterModifier
- GetConstructorImpl(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetConstructors()
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
Searches for a constructor whose parameters match the specified argument types, using the specified binding constraints.
public:
System::Reflection::ConstructorInfo ^ GetConstructor(System::Reflection::BindingFlags bindingAttr, cli::array <Type ^> ^ types);
public System.Reflection.ConstructorInfo? GetConstructor (System.Reflection.BindingFlags bindingAttr, Type[] types);
member this.GetConstructor : System.Reflection.BindingFlags * Type[] -> System.Reflection.ConstructorInfo
Public Function GetConstructor (bindingAttr As BindingFlags, types As Type()) As ConstructorInfo
Parameters
- bindingAttr
- BindingFlags
A bitwise combination of the enumeration values that specify how the search is conducted.
-or- Default to return null
.
- types
- Type[]
An array of Type objects representing the number, order, and type of the parameters for the constructor to get. -or- An empty array of the type Type (that is, Type[] types = Array.Empty{Type}()) to get a constructor that takes no parameters. -or- EmptyTypes.
Returns
A ConstructorInfo object representing the constructor that matches the specified requirements, if found; otherwise, null
.
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | 6, 7, 8, 9 |
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
Searches for a public instance constructor whose parameters match the types in the specified array.
public:
System::Reflection::ConstructorInfo ^ GetConstructor(cli::array <Type ^> ^ types);
public:
virtual System::Reflection::ConstructorInfo ^ GetConstructor(cli::array <Type ^> ^ types);
public System.Reflection.ConstructorInfo? GetConstructor (Type[] types);
public System.Reflection.ConstructorInfo GetConstructor (Type[] types);
[System.Runtime.InteropServices.ComVisible(true)]
public System.Reflection.ConstructorInfo GetConstructor (Type[] types);
member this.GetConstructor : Type[] -> System.Reflection.ConstructorInfo
abstract member GetConstructor : Type[] -> System.Reflection.ConstructorInfo
override this.GetConstructor : Type[] -> System.Reflection.ConstructorInfo
[<System.Runtime.InteropServices.ComVisible(true)>]
abstract member GetConstructor : Type[] -> System.Reflection.ConstructorInfo
override this.GetConstructor : Type[] -> System.Reflection.ConstructorInfo
Public Function GetConstructor (types As Type()) As ConstructorInfo
Parameters
- types
- Type[]
An array of Type objects representing the number, order, and type of the parameters for the desired constructor.
-or-
An empty array of Type objects, to get a constructor that takes no parameters. Such an empty array is provided by the static
field EmptyTypes.
Returns
An object representing the public instance constructor whose parameters match the types in the parameter type array, if found; otherwise, null
.
Implements
- Attributes
Exceptions
types
is multidimensional.
Examples
The following example obtains the type of MyClass
, gets the ConstructorInfo object, and displays the constructor signature.
using namespace System;
using namespace System::Reflection;
using namespace System::Security;
public ref class MyClass1
{
public:
MyClass1(){}
MyClass1( int i ){}
};
int main()
{
try
{
Type^ myType = MyClass1::typeid;
array<Type^>^types = gcnew array<Type^>(1);
types[ 0 ] = int::typeid;
// Get the constructor that takes an integer as a parameter.
ConstructorInfo^ constructorInfoObj = myType->GetConstructor( types );
if ( constructorInfoObj != nullptr )
{
Console::WriteLine( "The constructor of MyClass1 that takes an integer as a parameter is: " );
Console::WriteLine( constructorInfoObj );
}
else
{
Console::WriteLine( "The constructor of MyClass1 that takes an integer as a parameter is not available." );
}
}
catch ( Exception^ e )
{
Console::WriteLine( "Exception caught." );
Console::WriteLine( "Source: {0}", e->Source );
Console::WriteLine( "Message: {0}", e->Message );
}
}
using System;
using System.Reflection;
public class MyClass1
{
public MyClass1() { }
public MyClass1(int i) { }
public static void Main()
{
try
{
Type myType = typeof(MyClass1);
Type[] types = new Type[1];
types[0] = typeof(int);
// Get the constructor that takes an integer as a parameter.
ConstructorInfo constructorInfoObj = myType.GetConstructor(types);
if (constructorInfoObj != null)
{
Console.WriteLine("The constructor of MyClass1 that takes an " +
"integer as a parameter is: ");
Console.WriteLine(constructorInfoObj.ToString());
}
else
{
Console.WriteLine("The constructor of MyClass1 that takes an integer " +
"as a parameter is not available.");
}
}
catch (Exception e)
{
Console.WriteLine("Exception caught.");
Console.WriteLine("Source: " + e.Source);
Console.WriteLine("Message: " + e.Message);
}
}
}
type MyClass1() =
new (i: int) = MyClass1()
try
let myType = typeof<MyClass1>
let types = [| typeof<int> |]
// Get the constructor that takes an integer as a parameter.
let constructorInfoObj = myType.GetConstructor types
if constructorInfoObj <> null then
printfn "The constructor of MyClass1 that takes an integer as a parameter is: \n{constructorInfoObj}"
else
printfn "The constructor of MyClass1 that takes an integer as a parameter is not available."
with e ->
printfn "Exception caught."
printfn $"Source: {e.Source}"
printfn $"Message: {e.Message}"
Imports System.Reflection
Imports System.Security
Public Class MyClass1
Public Sub New()
End Sub
Public Sub New(ByVal i As Integer)
End Sub
Public Shared Sub Main()
Try
Dim myType As Type = GetType(MyClass1)
Dim types(0) As Type
types(0) = GetType(Int32)
' Get the constructor that takes an integer as a parameter.
Dim constructorInfoObj As ConstructorInfo = myType.GetConstructor(types)
If Not (constructorInfoObj Is Nothing) Then
Console.WriteLine("The constructor of MyClass that takes an integer as a parameter is: ")
Console.WriteLine(constructorInfoObj.ToString())
Else
Console.WriteLine("The constructor of MyClass that takes no " + "parameters is not available.")
End If
Catch e As Exception
Console.WriteLine("Exception caught.")
Console.WriteLine(("Source: " + e.Source))
Console.WriteLine(("Message: " + e.Message))
End Try
End Sub
End Class
Remarks
This method overload looks for public instance constructors and cannot be used to obtain a class initializer (static constructor). To get a class initializer, use an overload that takes BindingFlags, and specify BindingFlags.Static | BindingFlags.NonPublic (BindingFlags.StaticOr
BindingFlags.NonPublic in Visual Basic). You can also get the class initializer using the TypeInitializer property.
If the requested constructor is non-public, this method returns null
.
Note
You cannot omit parameters when looking up constructors and methods. You can only omit parameters when invoking.
If the current Type represents a constructed generic type, this method returns the ConstructorInfo with the type parameters replaced by the appropriate type arguments. If the current Type represents a type parameter in the definition of a generic type or generic method, this method always returns null
.
See also
- ConstructorInfo
- DefaultBinder
- GetConstructorImpl(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetConstructors()
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: