Type.GetConstructors() 方法用于获取 Type 对象的构造函数。该方法的重载列表中有2个方法如下:
Type.GetConstructors() Method
此方法用于返回为当前 Type 定义的所有公共构造函数。
用法:
public System.Reflection.ConstructorInfo[] GetConstructors ();
返回值:该方法返回 ConstructorInfo 数组对象,表示为当前 Type 定义的所有公共实例构造函数,但不包括类型初始化器(静态构造函数)。以下示例程序旨在说明 Type.GetConstructors() 方法的使用:示例 1:
C#
// C# program to demonstrate the
// Type.GetConstructors() Method
using System;
using System.Globalization;
using System.Reflection;
class GFG {
// Main Method
public static void Main()
{
// Declaring and initializing object
object obj = new Object();
// Getting the type of object
// using GetType() method
Type type = obj.GetType();
// Getting constructors of the current Type.
// using GetArrayRank() Method
ConstructorInfo[] info = type.GetConstructors();
// Display all the public instance constructors
Console.WriteLine("All constructors are shown below");
for (int i = 0; i < info.Length; i++)
Console.WriteLine(info[i]);
}
}
All constructors are shown below Void .ctor()
范例2:
C#
// C# program to demonstrate the
// Type.GetConstructors() Method
using System;
using System.Globalization;
using System.Reflection;
class GFG {
// Main Method
public static void Main()
{
// Declaring and initializing Type object
Type type = typeof(string);
// Getting constructors of the current Type.
// using GetConstructors() Method
ConstructorInfo[] info = type.GetConstructors();
// Display all the public instance constructors
Console.WriteLine("All constructors are shown below");
for (int i = 0; i < info.Length; i++)
Console.WriteLine(info[i]);
}
}
All constructors are shown below Void .ctor(Char[]) Void .ctor(Char[], Int32, Int32) Void .ctor(Char*) Void .ctor(Char*, Int32, Int32) Void .ctor(SByte*) Void .ctor(SByte*, Int32, Int32) Void .ctor(SByte*, Int32, Int32, Encoding) Void .ctor(Char, Int32) Void .ctor(ReadOnlySpan`1)
Type.GetConstructors(BindingFlags) Method
此方法用于返回为当前类型定义的构造函数,在派生类中重写时使用指定的 BindingFlags,
用法:
public abstract System.Reflection.ConstructorInfo[] GetConstructors (System.Reflection.BindingFlags bindingAttr);
在这里,它需要一个位掩码,该位掩码由一个或多个 BindingFlags 组成,指定在哪个点进行参加。或零,返回空值。
下面是一些 BindingFlags 过滤器标志,可用于定义要在搜索中包含哪些构造函数:
- 您必须指定 BindingFlags.Instance 或 BindingFlags.Static 才能获得返回。
- 指定 BindingFlags.Public 以在搜索中包含公共构造函数。
- 指定 BindingFlags.NonPublic 以在搜索中包括非公共构造函数(即私有、内部和受保护的构造函数)。不返回基类的构造函数。
返回值:此方法返回一个 ConstructorInfo 对象数组,表示为当前 Type 定义的与指定绑定约束匹配的所有构造函数,包括类型初始值设定项(如果已定义)。以下示例程序旨在说明上述方法的使用: 示例 1:
C#
// C# program to demonstrate the
// Type.GetConstructors(BindingFlags)
// Method
using System;
using System.Globalization;
using System.Reflection;
class GFG {
// Main Method
public static void Main()
{
// Declaring and initializing Type object
Type type = typeof(string);
// Declaring and initializing BindingFlags
// it is used to specify how the search
// is conducted
BindingFlags bf = BindingFlags.Public
| BindingFlags.Static
| BindingFlags.NonPublic
| BindingFlags.Instance;
// Getting constructors of the current Type.
// using GetConstructors() Method
ConstructorInfo[] info = type.GetConstructors(bf);
// Display all constructors
Console.WriteLine("All constructors are shown below");
for (int i = 0; i < info.Length; i++)
Console.WriteLine(info[i]);
}
}
All constructors are shown below Void .ctor(Char[]) Void .ctor(Char[], Int32, Int32) Void .ctor(Char*) Void .ctor(Char*, Int32, Int32) Void .ctor(SByte*) Void .ctor(SByte*, Int32, Int32) Void .ctor(SByte*, Int32, Int32, Encoding) Void .ctor(Char, Int32) Void .ctor(ReadOnlySpan`1)
范例2:
C#
// C# program to demonstrate the
// Type.GetConstructors(BindingFlags)
// Method
using System;
using System.Globalization;
using System.Reflection;
class GFG {
// Main Method
public static void Main()
{
// Creating and initializing object
object obj = new Object();
// Declaring and initializing Type object
Type type = obj.GetType();
// Declaring and initializing BindingFlags
// it is used to specify how the search is conducted
BindingFlags bf = BindingFlags.Public
| BindingFlags.Static
| BindingFlags.NonPublic
| BindingFlags.Instance;
// Getting constructors of the current Type.
// using GetConstructors() Method
ConstructorInfo[] info = type.GetConstructors(bf);
// Display all constructors
Console.WriteLine("All constructors are shown below");
for (int i = 0; i < info.Length; i++)
Console.WriteLine(info[i]);
}
}
All constructors are shown below Void .ctor()
参考:
相关用法
- C# Array.GetValue()方法用法及代码示例
- C# File.GetLastWriteTimeUtc()用法及代码示例
- C# MathF.Sin()用法及代码示例
- C# Double.CompareTo用法及代码示例
- C# UInt16.GetHashCode用法及代码示例
- C# Int64.CompareTo用法及代码示例
- C# MathF.Truncate()用法及代码示例
- C# MathF.Exp()用法及代码示例
- C# Array.BinarySearch(Array, Object)用法及代码示例
- C# Char.GetHashCode()用法及代码示例
- C# Char.GetTypeCode()用法及代码示例
- C# Object.GetHashCode()用法及代码示例
- C# Object.GetTypeCode()用法及代码示例
- C# Array.BinarySearch(Array, Int32, Int32, Object)用法及代码示例
- C# Stack.ToString()用法及代码示例
- C# Graphics.Clear()用法及代码示例
- C# List.FindIndex()用法及代码示例
- C# Double.Equals()用法及代码示例
- C# Decimal.GetTypeCode用法及代码示例
- C# Decimal.GetHashCode用法及代码示例
- C# Graphics.DrawArc()用法及代码示例
注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 Type.GetConstructors() Method in C# with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。