Type.GetConstructor 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取当前 Type构造函数的特定构造函数。
重载
| 名称 | 说明 |
|---|---|
| GetConstructor(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[]) |
使用指定的绑定约束和指定的调用约定搜索其参数与指定参数类型和修饰符匹配的构造函数。 |
| GetConstructor(BindingFlags, Binder, Type[], ParameterModifier[]) |
使用指定的绑定约束搜索其参数与指定参数类型和修饰符匹配的构造函数。 |
| GetConstructor(BindingFlags, Type[]) |
使用指定的绑定约束搜索其参数与指定参数类型匹配的构造函数。 |
| GetConstructor(Type[]) |
搜索其参数与指定数组中的类型匹配的公共实例构造函数。 |
GetConstructor(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
使用指定的绑定约束和指定的调用约定搜索其参数与指定参数类型和修饰符匹配的构造函数。
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);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
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);
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);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)>]
member this.GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.ConstructorInfo
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
参数
- binder
- Binder
一个对象,定义一组属性并启用绑定,这可能涉及选择重载的方法、强制参数类型以及通过反射调用成员。
-或-
使用 的 null 引用(Nothing 在 Visual Basic DefaultBinder中)。
- callConvention
- CallingConventions
指定用于参数顺序和布局的规则集、传递返回值的方式、用于参数的寄存器以及清理堆栈的对象。
- types
- Type[]
一个 Type 对象数组,表示要获取的构造函数的参数的数量、顺序和类型。
-或-
类型(即 Type[] types = new Type[0])的 Type 空数组,用于获取不带参数的构造函数。
- modifiers
- ParameterModifier[]
表示与数组中types相应元素关联的属性的对象数组ParameterModifier。 默认绑定器不处理此参数。
返回
一个对象,表示符合指定要求的构造函数(如果找到);否则,为 null.
实现
- 属性
例外
示例
下面的示例获取类型 MyClass、获取 ConstructorInfo 对象并显示构造函数签名。
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
注解
尽管默认联编程序不处理 ParameterModifier ( modifiers 参数),但可以使用抽象 System.Reflection.Binder 类编写一个处理自定义 modifiers联编程序。
ParameterModifier 仅在通过 COM 互作调用时使用,并且仅处理通过引用传递的参数。
如果完全匹配不存在, binder 将尝试强制指定在数组中指定的 types 参数类型,以便选择匹配项。
binder如果无法选择匹配项,则null返回。
以下 BindingFlags 筛选器标志可用于定义要在搜索中包含哪些构造函数:
您必须指定
BindingFlags.Instance或BindingFlags.Static才能获得返回结果。指定
BindingFlags.Public在搜索中包含公共构造函数。指定
BindingFlags.NonPublic在搜索中包含非公共构造函数(即专用构造函数、内部构造函数和受保护的构造函数)。
有关详细信息,请参阅 System.Reflection.BindingFlags。
若要使用此方法获取类初始值设定项(静态构造函数),必须指定 BindingFlags.Static | BindingFlags.NonPublic (BindingFlags.StaticOrBindingFlags.NonPublic 在 Visual Basic 中)。 还可以使用 TypeInitializer 属性获取类初始值设定项。
下表显示了通过 Get 方法在反射类型时返回的基类成员。
| 成员类型 | Static | 非静态 |
|---|---|---|
| 构造函数 | 否 | 否 |
| 领域 | 否 | 是的。 字段始终是按名称和签名隐藏的。 |
| 事件 | 不適用 | 常见的类型系统规则是继承与实现属性的方法相同。 反射将属性视为按名称和签名隐藏。 请参阅下面的说明 2。 |
| 方法 | 否 | 是的。 方法(虚拟和非虚拟)可以按名称隐藏或按名称和签名隐藏。 |
| 嵌套类型 | 否 | 否 |
| 财产 | 不適用 | 常见的类型系统规则是继承与实现属性的方法相同。 反射将属性视为按名称和签名隐藏。 请参阅下面的说明 2。 |
按名称和签名隐藏会考虑签名的所有部分,包括自定义修饰符、返回类型、参数类型、sentinel 和非托管调用约定。 这是二进制比较。
对于反射,属性和事件通过名称和签名进行隐藏。 如果基类中同时具有 get 和 set 访问器的属性,但派生类只有 get 访问器,则派生类属性将隐藏基类属性,并且无法访问基类上的 setter。
自定义属性不是常见类型系统的一部分。
注释
在查找构造函数和方法时,不能省略参数。 只能在调用时省略参数。
如果当前 Type 表示已构造的泛型类型,则此方法返回的 ConstructorInfo 中,类型参数已被相应的类型实参替换。 如果当前 Type 表示泛型类型或泛型方法定义中的类型参数,则此方法始终返回 null。
另请参阅
- ConstructorInfo
- BindingFlags
- Binder
- DefaultBinder
- CallingConventions
- ParameterModifier
- GetConstructorImpl(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetConstructors()
适用于
GetConstructor(BindingFlags, Binder, Type[], ParameterModifier[])
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
使用指定的绑定约束搜索其参数与指定参数类型和修饰符匹配的构造函数。
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);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
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);
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);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)>]
member this.GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.ConstructorInfo
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
参数
- binder
- Binder
一个对象,定义一组属性并启用绑定,这可能涉及选择重载的方法、强制参数类型以及通过反射调用成员。
-或-
使用 的 null 引用(Nothing 在 Visual Basic DefaultBinder中)。
- types
- Type[]
一个 Type 对象数组,表示要获取的构造函数的参数的数量、顺序和类型。
-或-
类型(即 Type[] types = new Type[0])的 Type 空数组,用于获取不带参数的构造函数。
-或-
- modifiers
- ParameterModifier[]
ParameterModifier表示与参数类型数组中相应元素关联的属性的对象数组。 默认绑定器不处理此参数。
返回
一个 ConstructorInfo 对象,表示与指定要求匹配的构造函数的对象,如果找到,则为 ;否则为 null。
实现
- 属性
例外
示例
下面的示例获取类型 MyClass、获取 ConstructorInfo 对象并显示构造函数签名。
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
注解
如果完全匹配不存在, binder 将尝试强制指定在数组中指定的 types 参数类型,以便选择匹配项。
binder如果无法选择匹配项,则null返回。
以下 BindingFlags 筛选器标志可用于定义要在搜索中包含哪些构造函数:
您必须指定
BindingFlags.Instance或BindingFlags.Static才能获得返回结果。指定
BindingFlags.Public在搜索中包含公共构造函数。指定
BindingFlags.NonPublic在搜索中包含非公共构造函数(即专用构造函数、内部构造函数和受保护的构造函数)。
有关详细信息,请参阅 System.Reflection.BindingFlags。
若要使用此方法重载获取类初始值设定项(静态构造函数),必须指定 BindingFlags.Static | BindingFlags.NonPublic (BindingFlags.StaticOrBindingFlags.NonPublic 在 Visual Basic 中)。 还可以使用 TypeInitializer 属性获取类初始值设定项。
注释
在查找构造函数和方法时,不能省略参数。 只能在调用时省略参数。
如果当前 Type 表示已构造的泛型类型,则此方法返回的 ConstructorInfo 中,类型参数已被相应的类型实参替换。 如果当前 Type 表示泛型类型或泛型方法定义中的类型参数,则此方法始终返回 null。
另请参阅
- ConstructorInfo
- BindingFlags
- Binder
- DefaultBinder
- ParameterModifier
- GetConstructorImpl(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetConstructors()
适用于
GetConstructor(BindingFlags, Type[])
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
使用指定的绑定约束搜索其参数与指定参数类型匹配的构造函数。
public:
System::Reflection::ConstructorInfo ^ GetConstructor(System::Reflection::BindingFlags bindingAttr, cli::array <Type ^> ^ types);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
public System.Reflection.ConstructorInfo? GetConstructor(System.Reflection.BindingFlags bindingAttr, Type[] types);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)>]
member this.GetConstructor : System.Reflection.BindingFlags * Type[] -> System.Reflection.ConstructorInfo
Public Function GetConstructor (bindingAttr As BindingFlags, types As Type()) As ConstructorInfo
参数
- bindingAttr
- BindingFlags
枚举值的按位组合,用于指定如何执行搜索。
- 或 - 默认返回 null。
- types
- Type[]
一个 Type 对象的数组,表示要获取的构造函数的参数的数量、顺序和类型。 - 或 - 类型(即 Type[] types = Array.Empty{Type}()的空数组 Type ,以获取不带任何参数的构造函数。 -or- EmptyTypes.
返回
一个 ConstructorInfo 对象,表示与指定要求匹配的构造函数的对象,如果找到,则为 ;否则为 null。
- 属性
适用于
GetConstructor(Type[])
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
搜索其参数与指定数组中的类型匹配的公共实例构造函数。
public:
System::Reflection::ConstructorInfo ^ GetConstructor(cli::array <Type ^> ^ types);
public:
virtual System::Reflection::ConstructorInfo ^ GetConstructor(cli::array <Type ^> ^ types);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
public System.Reflection.ConstructorInfo? GetConstructor(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);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)>]
member this.GetConstructor : Type[] -> System.Reflection.ConstructorInfo
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
参数
- types
- Type[]
一个 Type 对象数组,表示所需构造函数的参数的数量、顺序和类型。
-或-
一个空的对象 Type 数组,用于获取不带参数的构造函数。 字段提供staticEmptyTypes此类空数组。
返回
一个对象,表示公共实例构造函数,其参数与参数类型数组中的类型匹配(如果找到);否则,为 null.
实现
- 属性
例外
types 是多维。
示例
下面的示例获取类型 MyClass、获取 ConstructorInfo 对象并显示构造函数签名。
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
注解
此方法重载查找公共实例构造函数,不能用于获取类初始值设定项(静态构造函数)。 若要获取类初始值设定项,请使用采用BindingFlags和指定(BindingFlags.StaticBindingFlags.NonPublicOr在 Visual Basic 中)BindingFlags.Static | BindingFlags.NonPublic的重载。 还可以使用 TypeInitializer 属性获取类初始值设定项。
如果请求的构造函数为非公共构造函数,则此方法返回 null。
注释
在查找构造函数和方法时,不能省略参数。 只能在调用时省略参数。
如果当前 Type 表示已构造的泛型类型,则此方法返回的 ConstructorInfo 中,类型参数已被相应的类型实参替换。 如果当前 Type 表示泛型类型或泛型方法定义中的类型参数,则此方法始终返回 null。
另请参阅
- ConstructorInfo
- DefaultBinder
- GetConstructorImpl(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetConstructors()