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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。