ConstructorInfo 類別

定義

探索類別建構函式的屬性,並提供建構函式中繼資料的存取。

public ref class ConstructorInfo abstract : System::Reflection::MethodBase
public ref class ConstructorInfo abstract : System::Reflection::MethodBase, System::Runtime::InteropServices::_ConstructorInfo
public abstract class ConstructorInfo : System.Reflection.MethodBase
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Serializable]
public abstract class ConstructorInfo : System.Reflection.MethodBase, System.Runtime.InteropServices._ConstructorInfo
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class ConstructorInfo : System.Reflection.MethodBase, System.Runtime.InteropServices._ConstructorInfo
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class ConstructorInfo : System.Reflection.MethodBase
type ConstructorInfo = class
    inherit MethodBase
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Serializable>]
type ConstructorInfo = class
    inherit MethodBase
    interface _ConstructorInfo
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ConstructorInfo = class
    inherit MethodBase
    interface _ConstructorInfo
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ConstructorInfo = class
    inherit MethodBase
Public MustInherit Class ConstructorInfo
Inherits MethodBase
Public MustInherit Class ConstructorInfo
Inherits MethodBase
Implements _ConstructorInfo
繼承
ConstructorInfo
衍生
屬性
實作

範例

下列範例會使用 ConstructorInfoGetConstructorBindingFlags 來尋找符合指定搜尋準則的建構函式。

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 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 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 MyClass1 that is a public " +
                    "instance method and takes an integer as a parameter is: ");
                Console.WriteLine(constructorInfoObj.ToString());
            }
            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: " + 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);
        }
    }
}
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

備註

ConstructorInfo 用來探索建構函式的屬性,以及叫用建構函式。 物件是藉由呼叫 InvokeConstructorInfo 物件的 或 GetConstructor 方法所 GetConstructors 傳回的 Type 來建立。

注意

ConstructorInfo 繼承自數個 MethodBase 成員,例如 IsGenericMethod 可用來檢查泛型方法。 在 .NET Framework 2.0 版建構函式中不能是泛型的,因此這些成員會傳回 falsenull

給實施者的注意事項

當您繼承自 ConstructorInfo 時,必須覆寫下列成員多載: Invoke(BindingFlags, Binder, Object[], CultureInfo)

建構函式

ConstructorInfo()

初始化 ConstructorInfo 類別的新執行個體。

欄位

ConstructorName

表示類別建構函式方法儲存在中繼資料時的名稱。 這個名稱永遠是「.ctor」。 此欄位為唯讀。

TypeConstructorName

表示型別建構函式方法儲存在中繼資料時的名稱。 這個名稱永遠是「.cctor」。 這個屬性是唯讀的。

屬性

Attributes

取得與這個方法相關的屬性 (Attribute)。

(繼承來源 MethodBase)
CallingConvention

取得值,指出這個方法的呼叫慣例。

(繼承來源 MethodBase)
ContainsGenericParameters

取得值,指出泛型方法是否包含未指派的泛型型別參數。

(繼承來源 MethodBase)
CustomAttributes

取得包含此成員之自訂屬性的集合。

(繼承來源 MemberInfo)
DeclaringType

取得宣告這個成員的類別。

(繼承來源 MemberInfo)
IsAbstract

取得值,指出方法是否為抽象。

(繼承來源 MethodBase)
IsAssembly

取得值,指出 Assembly 是否描述此方法或建構函式 (Constructor) 的潛在可視性;亦即,最多只有相同組件 (Assembly) 中的其他型別可以看見該方法或建構函式,組件外部的衍生型別 (Derived Type) 則看不見它們。

(繼承來源 MethodBase)
IsCollectible

取得指出此 MemberInfo 物件是否為可回收 AssemblyLoadContext 中保存之組件一部分的值。

(繼承來源 MemberInfo)
IsConstructedGenericMethod

探索類別建構函式的屬性,並提供建構函式中繼資料的存取。

(繼承來源 MethodBase)
IsConstructor

取得值,指出方法是否為建構函示。

(繼承來源 MethodBase)
IsFamily

取得值,指出 Family 是否描述此方法或建構函式的可視性;亦即,您只能在其類別和衍生類別內看見該方法或建構函式。

(繼承來源 MethodBase)
IsFamilyAndAssembly

取得值,指出 FamANDAssem 是否描述此方法或建構函式的可視性;亦即,只有當該方法或建構函式位於相同的組件時,衍生類別才能呼叫它們。

(繼承來源 MethodBase)
IsFamilyOrAssembly

取得值,指出 FamORAssem 是否描述此方法或建構函式的潛在可視性;亦即,無論該方法或建構函式位於何處,衍生類別以及相同組件中的類別都可以呼叫它們。

(繼承來源 MethodBase)
IsFinal

取得值,指出這個方法是否為 final

(繼承來源 MethodBase)
IsGenericMethod

取得值,指出方法是否為泛型。

(繼承來源 MethodBase)
IsGenericMethodDefinition

取得值,指出方法是否為泛型方法定義。

(繼承來源 MethodBase)
IsHideBySig

取得值,指出是否只有簽章完全一樣的同類成員隱藏於衍生類別中。

(繼承來源 MethodBase)
IsPrivate

取得值,指出這個成員是否為私用的 (Private)。

(繼承來源 MethodBase)
IsPublic

取得值,指出這是否為公用的方法。

(繼承來源 MethodBase)
IsSecurityCritical

取得值,這個值表示目前方法或建構函式在目前信任層級上是否為安全性關鍵或安全性安全關鍵,因而可以執行重要的作業。

(繼承來源 MethodBase)
IsSecuritySafeCritical

取得值,這個值表示目前方法或建構函式在目前信任層級上是否為安全性安全關鍵,也就是說,它是否可以執行重要作業並且可供透明程式碼存取。

(繼承來源 MethodBase)
IsSecurityTransparent

取得值,這個值表示目前方法或建構函式在目前信任層級上是否為透明,因此不得執行重要作業。

(繼承來源 MethodBase)
IsSpecialName

取得值,指出這個方法是否有特別的名稱。

(繼承來源 MethodBase)
IsStatic

取得值指出方法是否為 static

(繼承來源 MethodBase)
IsVirtual

取得值指出方法是否為 virtual

(繼承來源 MethodBase)
MemberType

取得 MemberTypes 值,表示這個成員為建構函式。

MemberType

在衍生類別中覆寫時,取得 MemberTypes 值,指出成員類型︰方法、建構函式、事件等。

(繼承來源 MemberInfo)
MetadataToken

取得值,這個值可識別中繼資料項目。

(繼承來源 MemberInfo)
MethodHandle

取得方法內部中繼資料 (Metadata) 表示的控制代碼。

(繼承來源 MethodBase)
MethodImplementationFlags

取得 MethodImplAttributes 旗標,這些旗標會指定方法實作的屬性。

(繼承來源 MethodBase)
Module

取得用於定義型別的模組,該型別宣告以目前 MemberInfo 表示的成員。

(繼承來源 MemberInfo)
Name

取得目前成員的名稱。

(繼承來源 MemberInfo)
ReflectedType

取得類別物件,是用來取得這個 MemberInfo 的執行個體。

(繼承來源 MemberInfo)

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 MethodBase)
GetCustomAttributes(Boolean)

在衍生類別中覆寫時,傳回套用至此成員之所有自訂屬性的陣列。

(繼承來源 MemberInfo)
GetCustomAttributes(Type, Boolean)

當在衍生的類別中覆寫時,會傳回套用至這個成員的自訂屬性陣列,並以 Type 識別。

(繼承來源 MemberInfo)
GetCustomAttributesData()

傳回 CustomAttributeData 物件的清單,表示已套用至目標成員之屬性的資料。

(繼承來源 MemberInfo)
GetGenericArguments()

傳回 Type 物件的陣列,這些物件代表泛型方法的類型引數,或泛型方法定義的類型參數。

(繼承來源 MethodBase)
GetHashCode()

傳回這個執行個體的雜湊碼。

GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 MethodBase)
GetMethodBody()

在衍生類別中覆寫時,取得 MethodBody 物件,其提供對目前方法之 MSIL 資料流、區域變數和例外狀況的存取。

(繼承來源 MethodBase)
GetMethodImplementationFlags()

在衍生類別中覆寫時,傳回 MethodImplAttributes 旗標。

(繼承來源 MethodBase)
GetParameters()

在衍生類別中覆寫時,取得指定方法或建構函式的參數。

(繼承來源 MethodBase)
GetType()

探索類別建構函式的屬性,並提供建構函式中繼資料的存取。

GetType()

探索類別建構函式的屬性,並提供建構函式中繼資料的存取。

(繼承來源 MethodBase)
HasSameMetadataDefinitionAs(MemberInfo)

探索類別建構函式的屬性,並提供建構函式中繼資料的存取。

(繼承來源 MemberInfo)
Invoke(BindingFlags, Binder, Object[], CultureInfo)

在衍生類別中實作時,請叫用在指定 ConstructorInfo 條件約束 (Constraint) 下有指定引數的這個 Binder 所反映的建構函式。

Invoke(Object, BindingFlags, Binder, Object[], CultureInfo)

在衍生類別中覆寫時,需使用指定的參數叫用反映的方法或建構函式。

(繼承來源 MethodBase)
Invoke(Object, Object[])

使用指定的參數叫用由目前執行個體代表的方法或建構函式。

(繼承來源 MethodBase)
Invoke(Object[])

叫用包含指定參數的執行個體所反映的建構函式,為不常用的參數提供預設值。

IsDefined(Type, Boolean)

在衍生類別中覆寫時,表示是否已有一個或多個具有指定型別或其衍生型別的屬性套用至這個成員。

(繼承來源 MemberInfo)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

運算子

Equality(ConstructorInfo, ConstructorInfo)

表示兩個 ConstructorInfo 物件是否相等。

Inequality(ConstructorInfo, ConstructorInfo)

表示兩個 ConstructorInfo 物件是否不相等。

明確介面實作

_ConstructorInfo.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

_ConstructorInfo.GetType()

取得 Type 物件,其代表 ConstructorInfo 類型。

_ConstructorInfo.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

_ConstructorInfo.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

_ConstructorInfo.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

_ConstructorInfo.Invoke_2(Object, BindingFlags, Binder, Object[], CultureInfo)

為 COM 物件提供與版本無關的 Invoke(Object, BindingFlags, Binder, Object[], CultureInfo) 方法之存取權。

_ConstructorInfo.Invoke_3(Object, Object[])

為 COM 物件提供與版本無關的 Invoke(Object, Object[]) 方法之存取權。

_ConstructorInfo.Invoke_4(BindingFlags, Binder, Object[], CultureInfo)

為 COM 物件提供與版本無關的 Invoke(BindingFlags, Binder, Object[], CultureInfo) 方法之存取權。

_ConstructorInfo.Invoke_5(Object[])

為 COM 物件提供與版本無關的 Invoke(Object[]) 方法之存取權。

_MemberInfo.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 MemberInfo)
_MemberInfo.GetType()

取得 Type 物件,表示 MemberInfo 類別。

(繼承來源 MemberInfo)
_MemberInfo.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 MemberInfo)
_MemberInfo.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 MemberInfo)
_MemberInfo.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 MemberInfo)
_MethodBase.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 MethodBase)
_MethodBase.GetType()

如需這個成員的說明,請參閱 GetType()

(繼承來源 MethodBase)
_MethodBase.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 MethodBase)
_MethodBase.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 MethodBase)
_MethodBase.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 MethodBase)
_MethodBase.IsAbstract

如需這個成員的說明,請參閱 IsAbstract

(繼承來源 MethodBase)
_MethodBase.IsAssembly

如需這個成員的說明,請參閱 IsAssembly

(繼承來源 MethodBase)
_MethodBase.IsConstructor

如需這個成員的說明,請參閱 IsConstructor

(繼承來源 MethodBase)
_MethodBase.IsFamily

如需這個成員的說明,請參閱 IsFamily

(繼承來源 MethodBase)
_MethodBase.IsFamilyAndAssembly

如需這個成員的說明,請參閱 IsFamilyAndAssembly

(繼承來源 MethodBase)
_MethodBase.IsFamilyOrAssembly

如需這個成員的說明,請參閱 IsFamilyOrAssembly

(繼承來源 MethodBase)
_MethodBase.IsFinal

如需這個成員的說明,請參閱 IsFinal

(繼承來源 MethodBase)
_MethodBase.IsHideBySig

如需這個成員的說明,請參閱 IsHideBySig

(繼承來源 MethodBase)
_MethodBase.IsPrivate

如需這個成員的說明,請參閱 IsPrivate

(繼承來源 MethodBase)
_MethodBase.IsPublic

如需這個成員的說明,請參閱 IsPublic

(繼承來源 MethodBase)
_MethodBase.IsSpecialName

如需這個成員的說明,請參閱 IsSpecialName

(繼承來源 MethodBase)
_MethodBase.IsStatic

如需這個成員的說明,請參閱 IsStatic

(繼承來源 MethodBase)
_MethodBase.IsVirtual

如需這個成員的說明,請參閱 IsVirtual

(繼承來源 MethodBase)
ICustomAttributeProvider.GetCustomAttributes(Boolean)

傳回這個成員中定義的所有自訂屬性的陣列 (但具名屬性除外),如果沒有自訂屬性,則傳回空陣列。

(繼承來源 MemberInfo)
ICustomAttributeProvider.GetCustomAttributes(Type, Boolean)

傳回這個成員中定義的自訂屬性陣列 (依類型識別),如果沒有該類型的自訂屬性,則傳回空陣列。

(繼承來源 MemberInfo)
ICustomAttributeProvider.IsDefined(Type, Boolean)

指出此成員上是否有定義一個或多個 attributeType 執行個體。

(繼承來源 MemberInfo)

擴充方法

GetCustomAttribute(MemberInfo, Type)

擷取指定型別的自訂屬性,此屬性套用至指定成員。

GetCustomAttribute(MemberInfo, Type, Boolean)

擷取只訂型別的自訂屬性,此屬性套用至指定成員,並且可選擇性檢查該成員的祖系。

GetCustomAttribute<T>(MemberInfo)

擷取指定型別的自訂屬性,此屬性套用至指定成員。

GetCustomAttribute<T>(MemberInfo, Boolean)

擷取只訂型別的自訂屬性,此屬性套用至指定成員,並且可選擇性檢查該成員的祖系。

GetCustomAttributes(MemberInfo)

擷取套用至指定成員的自訂屬性集合。

GetCustomAttributes(MemberInfo, Boolean)

擷取自訂屬性集合,此集合套用至指定成員,並且可選擇性檢查該成員的祖系。

GetCustomAttributes(MemberInfo, Type)

擷取指定型別的自訂屬性集合,此集合套用至指定成員。

GetCustomAttributes(MemberInfo, Type, Boolean)

擷取指定型別的自訂屬性集合,此集合套用至指定成員,並且可選擇性檢查該成員的祖系。

GetCustomAttributes<T>(MemberInfo)

擷取指定型別的自訂屬性集合,此集合套用至指定成員。

GetCustomAttributes<T>(MemberInfo, Boolean)

擷取指定型別的自訂屬性集合,此集合套用至指定成員,並且可選擇性檢查該成員的祖系。

IsDefined(MemberInfo, Type)

指出是否將所指定型別的自訂屬性套用至指定的成員。

IsDefined(MemberInfo, Type, Boolean)

指出指定之型別的自訂屬性是否會套用至指定的成員,以及選擇性地套用到其上階。

GetMetadataToken(MemberInfo)

取得指定成員的中繼資料語彙基元 (如果有)。

HasMetadataToken(MemberInfo)

傳回值,指出所指定成員是否有可用的中繼資料語彙基元。

適用於

執行緒安全性

此型別具備執行緒安全。