📜  C#中的Type.GetConstructors()方法(带有示例)

📅  最后修改于: 2021-05-30 00:01:32             🧑  作者: Mango

Type.GetConstructors()方法用于获取Type对象的构造函数。此方法的重载列表中有2种方法,如下所示:

  • GetConstructors()方法
  • GetConstructors(BindingFlags)方法

Type.GetConstructors()方法

此方法用于返回为当前Type定义的所有公共构造函数。

句法:

public System.Reflection.ConstructorInfo[] GetConstructors ();

返回:此方法返回ConstructorInfo数组对象,该对象代表为当前Type定义的所有公共实例构造函数,但不包括类型initializer(静态构造函数)。

下面的程序说明了Type.GetConstructors()方法的用法:

范例1:

// 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 obj
        // 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# 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)方法

当在派生类中重写时,此方法用于使用指定的BindingFlags返回为当前Type定义的构造函数,

句法:

在这里,它需要一个位掩码,该位掩码包含一个或多个BindingFlags,这些BindingFlags指定进行参加的时间。或零,则返回null。

以下是一些BindingFlags过滤器标志,可用于定义要在搜索中包括的构造函数:

  • 您必须指定BindingFlags.InstanceBindingFlags.Static才能获得回报。
  • 指定BindingFlags.Public以在搜索中包括公共构造函数。
  • 指定BindingFlags.NonPublic在搜索中包括非公共构造函数(即私有,内部和受保护的构造函数)。不返回基类的构造方法。

返回值:该方法返回一个ConstructorInfo对象的数组,该对象表示为当前Type定义的,与指定的绑定约束匹配的所有构造函数,包括定义的类型初始值设定项。

下面的程序说明了上述方法的用法:

范例1:

// 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# 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()

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.type.getconstructors?view=netcore-3.0