Type.GetConstructor 方法

定義

取得目前 Type 的特定建構函式。Gets a specific constructor of the current Type.

多載

GetConstructor(Type[])

搜尋其參數符合在指定陣列中的類型的公用執行個體建構函式。Searches for a public instance constructor whose parameters match the types in the specified array.

GetConstructor(BindingFlags, Binder, Type[], ParameterModifier[])

使用指定的繫結條件約束 (Constraint) 搜尋其參數符合指定的引數類型和修飾詞 (Modifier) 的建構函式。Searches for a constructor whose parameters match the specified argument types and modifiers, using the specified binding constraints.

GetConstructor(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])

使用指定的繫結條件約束和指定的呼叫慣例,搜尋其參數符合指定的引數類型和修飾詞的建構函式。Searches for a constructor whose parameters match the specified argument types and modifiers, using the specified binding constraints and the specified calling convention.

GetConstructor(Type[])

搜尋其參數符合在指定陣列中的類型的公用執行個體建構函式。Searches for a public instance constructor whose parameters match the types in the specified array.

public:
 virtual System::Reflection::ConstructorInfo ^ GetConstructor(cli::array <Type ^> ^ types);
[System.Runtime.InteropServices.ComVisible(true)]
public System.Reflection.ConstructorInfo GetConstructor (Type[] types);
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 物件組成的陣列,表示所要建構函式參數的數目、順序和類型。An array of Type objects representing the number, order, and type of the parameters for the desired constructor.

-或--or- Type 物件組成的空陣列,用來取得不需任何參數的建構函式。An empty array of Type objects, to get a constructor that takes no parameters. 這種供陣列是由 static 欄位 EmptyTypes 提供的。Such an empty array is provided by the static field EmptyTypes.

傳回

物件,表示其參數符合參數類型陣列中之類型的公用執行個體建構函式 (如有找到);否則為 nullAn object representing the public instance constructor whose parameters match the types in the parameter type array, if found; otherwise, null.

實作

屬性

例外狀況

typesnulltypes is null.

-或--or- types 的其中一個項目為 nullOne of the elements in types is null.

types 是多維的。types is multidimensional.

範例

下列範例會MyClass ConstructorInfo取得的類型、取得物件,並顯示此函式簽章。The following example obtains the type of MyClass, gets the ConstructorInfo object, and displays the constructor signature.

using namespace System;
using namespace System::Reflection;
using namespace System::Security;
public ref class MyClass1
{
public:
   MyClass1(){}

   MyClass1( int i ){}

};

int main()
{
   try
   {
      Type^ myType = MyClass1::typeid;
      array<Type^>^types = gcnew array<Type^>(1);
      types[ 0 ] = int::typeid;
      
      // Get the constructor that takes an integer as a parameter.
      ConstructorInfo^ constructorInfoObj = myType->GetConstructor( types );
      if ( constructorInfoObj != nullptr )
      {
         Console::WriteLine( "The constructor of MyClass1 that takes an integer as a parameter is: " );
         Console::WriteLine( constructorInfoObj );
      }
      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: {0}", e->Source );
      Console::WriteLine( "Message: {0}", e->Message );
   }
}

using System;
using System.Reflection;
using System.Security;

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);
        }
    }
}
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

備註

這個方法多載會尋找公用實例的構造函式,而且不能用來取得類別初始化運算式(靜態的函式)。This method overload looks for public instance constructors and cannot be used to obtain a class initializer (static constructor). 若要取得類別初始化運算式,請BindingFlags使用採用的多載,並指定| BindingFlags.NonPublic BindingFlags.StaticBindingFlags.Static Or BindingFlags.NonPublic在 Visual Basic 中)。To get a class initializer, use an overload that takes BindingFlags, and specify BindingFlags.Static | BindingFlags.NonPublic (BindingFlags.StaticOrBindingFlags.NonPublic in Visual Basic). 您也可以使用TypeInitializer屬性來取得類別初始化運算式。You can also get the class initializer using the TypeInitializer property.

如果要求的函式為非公用,這個方法null會傳回。If the requested constructor is non-public, this method returns null.

注意

查閱函式和方法時,您不能省略參數。You cannot omit parameters when looking up constructors and methods. 叫用時,您只能省略參數。You can only omit parameters when invoking.

如果目前Type的表示結構化的泛型型別,這個方法ConstructorInfo會傳回,其中的類型參數已由適當的類型引數所取代。If the current Type represents a constructed generic type, this method returns the ConstructorInfo with the type parameters replaced by the appropriate type arguments. 如果目前Type的表示泛型型別或泛型方法定義中的類型參數,則這個方法null一律會傳回。If the current Type represents a type parameter in the definition of a generic type or generic method, this method always returns null.

另請參閱

GetConstructor(BindingFlags, Binder, Type[], ParameterModifier[])

使用指定的繫結條件約束 (Constraint) 搜尋其參數符合指定的引數類型和修飾詞 (Modifier) 的建構函式。Searches for a constructor whose parameters match the specified argument types and modifiers, using the specified binding constraints.

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.Runtime.InteropServices.ComVisible(true)]
public System.Reflection.ConstructorInfo GetConstructor (System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, Type[] types, System.Reflection.ParameterModifier[] modifiers);
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

參數

bindingAttr
BindingFlags

列舉值的位元組合,用來指定搜尋的執行方式。A bitwise combination of the enumeration values that specify how the search is conducted.

-或--or- 要傳回 nullDefaultDefault to return null.

binder
Binder

定義一組屬性並啟用繫結的物件,可包含多載方法的選擇、引數類型的強制,以及透過反映的成員引動過程。An object that defines a set of properties and enables binding, which can involve selection of an overloaded method, coercion of argument types, and invocation of a member through reflection.

-或--or- Null 參考 (在 Visual Basic 中為 Nothing),可使用 DefaultBinderA null reference (Nothing in Visual Basic), to use the DefaultBinder.

types
Type[]

Type 物件的陣列,代表所要取得之建構函式的參數數目、順序和類型。An array of Type objects representing the number, order, and type of the parameters for the constructor to get.

-或--or- 用以取得沒有參數的建構函式之 Type 類型的空陣列 (也就是,Type[] types = new Type[0])。An empty array of the type Type (that is, Type[] types = new Type[0]) to get a constructor that takes no parameters.

-或--or- EmptyTypes.EmptyTypes.

modifiers
ParameterModifier[]

ParameterModifier 物件的陣列,代表在參數類型陣列中與對應項目關聯的屬性。An array of ParameterModifier objects representing the attributes associated with the corresponding element in the parameter type array. 預設的繫結器不會處理這個參數。The default binder does not process this parameter.

傳回

ConstructorInfo 物件,代表符合指定之需求的建構函式 (如有找到);否則為 nullA ConstructorInfo object representing the constructor that matches the specified requirements, if found; otherwise, null.

實作

屬性

例外狀況

typesnulltypes is null.

-或--or- types 的其中一個項目為 nullOne of the elements in types is null.

types 是多維的。types is multidimensional.

-或--or- modifiers 是多維的。modifiers is multidimensional.

-或--or- typesmodifiers 的長度不同。types and modifiers do not have the same length.

範例

下列程式會取得MyClass1類別的類型、取得符合指定系結旗標的ConstructorInfo物件,並顯示此函式的簽章。The following program obtains the type of MyClass1 class, gets the ConstructorInfo object matching the specified binding flags, and displays the signature of the constructor.

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 constructor that is public and takes an integer parameter.
      ConstructorInfo^ constructorInfoObj = myType->GetConstructor( static_cast<BindingFlags>(BindingFlags::Instance | BindingFlags::Public), nullptr, types, nullptr );
      if ( constructorInfoObj != nullptr )
      {
         Console::WriteLine( "The constructor of MyClass1 that is public and takes an integer as a parameter is:" );
         Console::WriteLine( constructorInfoObj );
      }
      else
      {
         Console::WriteLine( "The constructor of the MyClass1 that is public 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 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 MyClass1 that is public " +
                    "and takes an integer as a parameter is:");
                Console.WriteLine(constructorInfoObj.ToString());
            }
            else
            {
                Console.WriteLine("The constructor of the MyClass1 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);
        }
    }
}
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陣列中指定的參數類型,以便選取相符的。If an exact match does not exist, the binder will attempt to coerce the parameter types specified in the types array in order to select a match. 如果無法選取相符項null ,則會傳回。 binderIf the binder is unable to select a match, then null is returned.

下列BindingFlags篩選旗標可以用來定義要包含在搜尋中的哪些函數:The following BindingFlags filter flags can be used to define which constructors to include in the search:

  • 您必須指定BindingFlags.InstanceBindingFlags.Static ,才能取得傳回。You must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return.

  • 指定BindingFlags.Public要在搜尋中包含公用的函式。Specify BindingFlags.Public to include public constructors in the search.

  • 指定BindingFlags.NonPublic在搜尋中包含非公用的函式(也就是私用、內部和受保護的函式)。Specify BindingFlags.NonPublic to include non-public constructors (that is, private, internal, and protected constructors) in the search.

如需詳細資訊,請參閱 System.Reflection.BindingFlagsSee System.Reflection.BindingFlags for more information.

若要使用這個方法多載來取得類別初始化運算式(靜態的函式BindingFlags.StaticBindingFlags.Static ,您必須指定| BindingFlags.NonPublicOr BindingFlags.NonPublic在 Visual Basic 中)。To get the class initializer (static constructor) using this method overload, you must specify BindingFlags.Static | BindingFlags.NonPublic (BindingFlags.StaticOrBindingFlags.NonPublic in Visual Basic). 您也可以使用TypeInitializer屬性來取得類別初始化運算式。You can also get the class initializer using the TypeInitializer property.

注意

查閱函式和方法時,您不能省略參數。You cannot omit parameters when looking up constructors and methods. 叫用時,您只能省略參數。You can only omit parameters when invoking.

如果目前Type的表示結構化的泛型型別,這個方法ConstructorInfo會傳回,其中的類型參數已由適當的類型引數所取代。If the current Type represents a constructed generic type, this method returns the ConstructorInfo with the type parameters replaced by the appropriate type arguments. 如果目前Type的表示泛型型別或泛型方法定義中的類型參數,則這個方法null一律會傳回。If the current Type represents a type parameter in the definition of a generic type or generic method, this method always returns null.

另請參閱

GetConstructor(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])

使用指定的繫結條件約束和指定的呼叫慣例,搜尋其參數符合指定的引數類型和修飾詞的建構函式。Searches for a constructor whose parameters match the specified argument types and modifiers, using the specified binding constraints and the specified calling convention.

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.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);
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

參數

bindingAttr
BindingFlags

列舉值的位元組合,用來指定搜尋的執行方式。A bitwise combination of the enumeration values that specify how the search is conducted.

-或--or- 要傳回 nullDefaultDefault to return null.

binder
Binder

定義一組屬性並啟用繫結的物件,可包含多載方法的選擇、引數類型的強制,以及透過反映的成員引動過程。An object that defines a set of properties and enables binding, which can involve selection of an overloaded method, coercion of argument types, and invocation of a member through reflection.

-或--or- Null 參考 (在 Visual Basic 中為 Nothing),可使用 DefaultBinderA null reference (Nothing in Visual Basic), to use the DefaultBinder.

callConvention
CallingConventions

物件,其指定一組所要使用的規則,而這些規則是關於引數的順序和配置、如何傳遞傳回值、引數使用哪些暫存器以及清除堆疊。The object that specifies the set of rules to use regarding the order and layout of arguments, how the return value is passed, what registers are used for arguments, and the stack is cleaned up.

types
Type[]

Type 物件的陣列,代表所要取得之建構函式的參數數目、順序和類型。An array of Type objects representing the number, order, and type of the parameters for the constructor to get.

-或--or- 用以取得沒有參數的建構函式之 Type 類型的空陣列 (也就是,Type[] types = new Type[0])。An empty array of the type Type (that is, Type[] types = new Type[0]) to get a constructor that takes no parameters.

modifiers
ParameterModifier[]

ParameterModifier 物件的陣列,代表在 types 陣列中與對應項目關聯的屬性。An array of ParameterModifier objects representing the attributes associated with the corresponding element in the types array. 預設的繫結器不會處理這個參數。The default binder does not process this parameter.

傳回

物件,代表符合指定之需求的建構函式 (如有找到);否則為 nullAn object representing the constructor that matches the specified requirements, if found; otherwise, null.

實作

屬性

例外狀況

typesnulltypes is null.

-或--or- types 的其中一個項目為 nullOne of the elements in types is null.

types 是多維的。types is multidimensional.

-或--or- modifiers 是多維的。modifiers is multidimensional.

-或--or- typesmodifiers 的長度不同。types and modifiers do not have the same length.

範例

下列範例會MyClass1 ConstructorInfo取得的類型、取得符合指定系結旗標的物件,並顯示此函式簽章。The following example obtains the type of MyClass1, gets the ConstructorInfo object that matches the specified binding flags, and displays the constructor signature.

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

備註

雖然預設的系結器不ParameterModifier會處理modifiers (參數),但您可以使用System.Reflection.Binder抽象類別來撰寫執行處理modifiers的自訂系結器。Although the default binder does not process ParameterModifier (the modifiers parameter), you can use the abstract System.Reflection.Binder class to write a custom binder that does process modifiers. ParameterModifier只有在透過 COM Interop 呼叫時才會使用,而且只會處理以傳址方式傳遞的參數。ParameterModifier is only used when calling through COM interop, and only parameters that are passed by reference are handled.

如果完全相符的binder不存在,將會嘗試強制轉型types陣列中指定的參數類型,以便選取相符的。If an exact match does not exist, the binder will attempt to coerce the parameter types specified in the types array in order to select a match. 如果無法選取相符項null ,則會傳回。 binderIf the binder is unable to select a match, then null is returned.

下列BindingFlags篩選旗標可以用來定義要包含在搜尋中的哪些函數:The following BindingFlags filter flags can be used to define which constructors to include in the search:

  • 您必須指定BindingFlags.InstanceBindingFlags.Static ,才能取得傳回。You must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return.

  • 指定BindingFlags.Public要在搜尋中包含公用的函式。Specify BindingFlags.Public to include public constructors in the search.

  • 指定BindingFlags.NonPublic在搜尋中包含非公用的函式(也就是私用、內部和受保護的函式)。Specify BindingFlags.NonPublic to include non-public constructors (that is, private, internal, and protected constructors) in the search.

如需詳細資訊,請參閱 System.Reflection.BindingFlagsSee System.Reflection.BindingFlags for more information.

若要使用這個方法取得類別初始化運算式(靜態的函式), BindingFlags.StaticBindingFlags.Static必須指定| BindingFlags.NonPublicOr BindingFlags.NonPublic在 Visual Basic 中)。To get the class initializer (static constructor) using this method, you must specify BindingFlags.Static | BindingFlags.NonPublic (BindingFlags.StaticOrBindingFlags.NonPublic in Visual Basic). 您也可以使用TypeInitializer屬性來取得類別初始化運算式。You can also get the class initializer using the TypeInitializer property.

下表顯示當在類型上反映時, Get方法所傳回之基類的成員。The following table shows what members of a base class are returned by the Get methods when reflecting on a type.

成員類型Member Type StaticStatic 非靜態Non-Static
建構函式Constructor No No
欄位Field No 可以。Yes. 欄位一律會依名稱和簽章隱藏。A field is always hide-by-name-and-signature.
Event - 事件Event 不適用Not applicable 一般類型系統規則是,繼承與實作為屬性的方法相同。The common type system rule is that the inheritance is the same as that of the methods that implement the property. 反映會將屬性視為逐名稱和簽章。Reflection treats properties as hide-by-name-and-signature. 請參閱下面的注2。See note 2 below.
方法Method No 可以。Yes. 方法(虛擬和非虛擬)可以依名稱或逐名稱和簽章來隱藏。A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature.
巢狀型別Nested Type No No
屬性Property 不適用Not applicable 一般類型系統規則是,繼承與實作為屬性的方法相同。The common type system rule is that the inheritance is the same as that of the methods that implement the property. 反映會將屬性視為逐名稱和簽章。Reflection treats properties as hide-by-name-and-signature. 請參閱下面的注2。See note 2 below.
  1. [逐名稱] 和 [簽章] 會考慮簽章的所有部分,包括自訂修飾詞、傳回類型、參數類型、sentinels 和非受控呼叫慣例。Hide-by-name-and-signature considers all of the parts of the signature, including custom modifiers, return types, parameter types, sentinels, and unmanaged calling conventions. 這是二進位比較。This is a binary comparison.

  2. 針對反映,屬性和事件是依名稱和簽章來隱藏。For reflection, properties and events are hide-by-name-and-signature. 如果您在基類中具有具有 get 和 set 存取子的屬性,但衍生類別只有 get 存取子,則衍生類別屬性會隱藏基類屬性,而且您將無法存取基類上的 setter。If you have a property with both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and you will not be able to access the setter on the base class.

  3. 自訂屬性不是一般型別系統的一部分。Custom attributes are not part of the common type system.

注意

查閱函式和方法時,您不能省略參數。You cannot omit parameters when looking up constructors and methods. 叫用時,您只能省略參數。You can only omit parameters when invoking.

如果目前Type的表示結構化的泛型型別,這個方法ConstructorInfo會傳回,其中的類型參數已由適當的類型引數所取代。If the current Type represents a constructed generic type, this method returns the ConstructorInfo with the type parameters replaced by the appropriate type arguments. 如果目前Type的表示泛型型別或泛型方法定義中的類型參數,則這個方法null一律會傳回。If the current Type represents a type parameter in the definition of a generic type or generic method, this method always returns null.

另請參閱

適用於