Type.GetMembers Method

Definition

取得目前 Type 的成員 (屬性、方法、欄位、事件等等)。Gets the members (properties, methods, fields, events, and so on) of the current Type.

Overloads

GetMembers()

傳回目前 Type 的所有公用成員。Returns all the public members of the current Type.

GetMembers(BindingFlags)

在衍生類別中覆寫時,使用指定的繫結條件約束,搜尋定義給目前 Type 的成員。When overridden in a derived class, searches for the members defined for the current Type, using the specified binding constraints.

GetMembers()

傳回目前 Type 的所有公用成員。Returns all the public members of the current Type.

public:
 cli::array <System::Reflection::MemberInfo ^> ^ GetMembers();
public:
 virtual cli::array <System::Reflection::MemberInfo ^> ^ GetMembers();
public System.Reflection.MemberInfo[] GetMembers ();
member this.GetMembers : unit -> System.Reflection.MemberInfo[]
abstract member GetMembers : unit -> System.Reflection.MemberInfo[]
override this.GetMembers : unit -> System.Reflection.MemberInfo[]
Public Function GetMembers () As MemberInfo()

Returns

MemberInfo[]

MemberInfo 物件的陣列,代表目前 Type 的所有公用成員。An array of MemberInfo objects representing all the public members of the current Type.

-或--or- MemberInfo 類型的空陣列 (如果目前 Type 沒有公用成員)。An empty array of type MemberInfo, if the current Type does not have public members.

Implements

Examples

下列程式碼範例示範如何使用 GetMembers() 方法多載,來收集指定類別的所有公用成員的相關資訊。The following code example demonstrates how to use the GetMembers() method overload to collect information about all public members of a specified class.

ref class MyClass
{
public:
   int myInt;
   String^ myString;
   MyClass(){}

   void Myfunction(){}

};

int main()
{
   try
   {
      MyClass^ myObject = gcnew MyClass;
      array<MemberInfo^>^myMemberInfo;
      
      // Get the type of 'MyClass'.
      Type^ myType = myObject->GetType();
      
      // Get the information related to all public members of 'MyClass'.
      myMemberInfo = myType->GetMembers();
      Console::WriteLine( "\nThe members of class '{0}' are :\n", myType );
      for ( int i = 0; i < myMemberInfo->Length; i++ )
      {
         
         // Display name and type of the concerned member.
         Console::WriteLine( "'{0}' is a {1}", myMemberInfo[ i ]->Name, myMemberInfo[ i ]->MemberType );

      }
   }
   catch ( SecurityException^ e ) 
   {
      Console::WriteLine( "Exception : {0}", e->Message );
   }

}

class MyClass
{
   public int myInt = 0;
   public string myString = null;

   public MyClass()
   {
   }
   public void Myfunction()
   {
   }
}

class Type_GetMembers
{
   public static void Main()
   {
      try
      {
         MyClass myObject = new MyClass();
         MemberInfo[] myMemberInfo;

         // Get the type of 'MyClass'.
         Type myType = myObject.GetType();

         // Get the information related to all public member's of 'MyClass'.
         myMemberInfo = myType.GetMembers();

         Console.WriteLine( "\nThe members of class '{0}' are :\n", myType);
         for (int i =0 ; i < myMemberInfo.Length ; i++)
         {
            // Display name and type of the concerned member.
            Console.WriteLine( "'{0}' is a {1}", myMemberInfo[i].Name, myMemberInfo[i].MemberType);
         }
      }
      catch(SecurityException e)
      {
         Console.WriteLine("Exception : " + e.Message );
      }
   }
}
Class [MyClass]
   Public myInt As Integer = 0
   Public myString As String = Nothing
   
   
   Public Sub New()
   End Sub
   
   Public Sub Myfunction()
   End Sub
End Class

Class Type_GetMembers
   
   Public Shared Sub Main()
      Try
         Dim myObject As New [MyClass]()
         Dim myMemberInfo() As MemberInfo
         
         ' Get the type of 'MyClass'.
         Dim myType As Type = myObject.GetType()
         
         ' Get the information related to all public member's of 'MyClass'. 
         myMemberInfo = myType.GetMembers()
         
         Console.WriteLine(ControlChars.Cr + "The members of class '{0}' are :" + ControlChars.Cr, myType)
         Dim i As Integer
         For i = 0 To myMemberInfo.Length - 1
            ' Display name and type of the concerned member.
            Console.WriteLine("'{0}' is a {1}", myMemberInfo(i).Name, myMemberInfo(i).MemberType)
         Next i

      Catch e As SecurityException
         Console.WriteLine(("Exception : " + e.Message.ToString()))
      End Try
   End Sub
End Class

Remarks

成員包括屬性、方法、函式、欄位、事件和巢狀型別。Members include properties, methods, constructors, fields, events, and nested types.

GetMembers 方法不會以特定順序傳回成員,例如依字母順序或宣告順序。The GetMembers method does not return members in a particular order, such as alphabetical or declaration order. 您的程式碼不能相依于傳回成員的順序,因為該順序會有所不同。Your code must not depend on the order in which members are returned, because that order varies.

這個方法多載會呼叫 GetMembers(BindingFlags) 方法多載, |並|以 BindingFlags.Public BindingFlags.Instance BindingFlags.StaticBindingFlags.PublicOrBindingFlags.InstanceOrBindingFlags.Static)。This method overload calls the GetMembers(BindingFlags) method overload, with BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static (BindingFlags.PublicOrBindingFlags.InstanceOrBindingFlags.Static in Visual Basic). 它找不到類別初始化運算式(靜態的函式)。It will not find class initializers (static constructors). 若要尋找類別初始化運算式,請呼叫 GetMembers(BindingFlags) 多載, |並在 Or中指定 BindingFlags.Static BindingFlags.NonPublicBindingFlags.StaticBindingFlags.NonPublic Visual Basic)。To find class initializers, call the GetMembers(BindingFlags) overload, and 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 靜態Static 非靜態Non-Static
建構函式Constructor No No
欄位Field No 是。Yes. 欄位一律會依名稱和簽章隱藏。A field is always hide-by-name-and-signature.
事件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.

如果目前的 Type 代表結構化的泛型型別,這個方法會傳回具有型別參數的 MemberInfo 物件,並由適當的型別引數取代。If the current Type represents a constructed generic type, this method returns the MemberInfo objects with the type parameters replaced by the appropriate type arguments.

如果目前的 Type 表示泛型型別或泛型方法定義中的類型參數,則這個方法會搜尋類別條件約束的成員,或如果沒有類別條件約束,則為 Object 的成員。If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the members of the class constraint, or the members of Object if there is no class constraint.

See also

GetMembers(BindingFlags)

在衍生類別中覆寫時,使用指定的繫結條件約束,搜尋定義給目前 Type 的成員。When overridden in a derived class, searches for the members defined for the current Type, using the specified binding constraints.

public:
 abstract cli::array <System::Reflection::MemberInfo ^> ^ GetMembers(System::Reflection::BindingFlags bindingAttr);
public abstract System.Reflection.MemberInfo[] GetMembers (System.Reflection.BindingFlags bindingAttr);
abstract member GetMembers : System.Reflection.BindingFlags -> System.Reflection.MemberInfo[]
Public MustOverride Function GetMembers (bindingAttr As BindingFlags) As MemberInfo()

Parameters

bindingAttr
BindingFlags

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

-或--or- 要傳回空陣列的 DefaultDefault to return an empty array.

Returns

MemberInfo[]

MemberInfo 物件的陣列,代表為目前 Type 定義之符合指定繫結條件約束的所有成員。An array of MemberInfo objects representing all members defined for the current Type that match the specified binding constraints.

-或--or- 如果未針對目前的 Type 定義任何成員,或沒有已定義的成員符合繫結條件約束,則為空陣列。An empty array if no members are defined for the current Type, or if none of the defined members match the binding constraints.

Implements

Examples

下列程式碼範例示範如何使用 GetMembers(BindingFlags) 方法多載,來收集指定類別之所有公用實例成員的相關資訊。The following code example demonstrates how to use the GetMembers(BindingFlags) method overload to collect information about all public instance members of a specified class.

ref class MyClass
{
public:
   int * myInt;
   String^ myString;
   MyClass(){}

   void Myfunction(){}

};

int main()
{
   try
   {
      MyClass^ MyObject = gcnew MyClass;
      array<MemberInfo^>^myMemberInfo;
      
      // Get the type of the class 'MyClass'.
      Type^ myType = MyObject->GetType();
      
      // Get the public instance members of the class 'MyClass'.
      myMemberInfo = myType->GetMembers( static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance) );
      Console::WriteLine( "\nThe public instance members of class '{0}' are : \n", myType );
      for ( int i = 0; i < myMemberInfo->Length; i++ )
      {
         
         // Display name and type of the member of 'MyClass'.
         Console::WriteLine( "'{0}' is a {1}", myMemberInfo[ i ]->Name, myMemberInfo[ i ]->MemberType );

      }
   }
   catch ( SecurityException^ e ) 
   {
      Console::WriteLine( "SecurityException : {0}", e->Message );
   }


      //Output:
      //The public instance members of class 'MyClass' are :

      //'Myfunction' is a Method
      //'ToString' is a Method
      //'Equals' is a Method
      //'GetHashCode' is a Method
      //'GetType' is a Method
      //'.ctor' is a Constructor
      //'myInt' is a Field
      //'myString' is a Field

}


class MyClass
{
   public int myInt = 0;
   public string myString = null;

   public MyClass()
   {
   }
   public void Myfunction()
   {
   }
}

class Type_GetMembers_BindingFlags
{
   public static void Main()
   {
      try
      {
         MyClass MyObject = new MyClass();
         MemberInfo [] myMemberInfo;

         // Get the type of the class 'MyClass'.
         Type myType = MyObject.GetType();

         // Get the public instance members of the class 'MyClass'.
         myMemberInfo = myType.GetMembers(BindingFlags.Public|BindingFlags.Instance);

         Console.WriteLine( "\nThe public instance members of class '{0}' are : \n", myType);
         for (int i =0 ; i < myMemberInfo.Length ; i++)
         {
            // Display name and type of the member of 'MyClass'.
            Console.WriteLine( "'{0}' is a {1}", myMemberInfo[i].Name, myMemberInfo[i].MemberType);
         }
      }
      catch (SecurityException e)
      {
         Console.WriteLine("SecurityException : " + e.Message );
      }

      //Output:
      //The public instance members of class 'MyClass' are :

      //'Myfunction' is a Method
      //'ToString' is a Method
      //'Equals' is a Method
      //'GetHashCode' is a Method
      //'GetType' is a Method
      //'.ctor' is a Constructor
      //'myInt' is a Field
      //'myString' is a Field
   }
}

Class [MyClass]
   Public myInt As Integer = 0
   Public myString As String = Nothing
   
   
   Public Sub New()
   End Sub
   
   Public Sub Myfunction()
   End Sub
End Class

Class Type_GetMembers_BindingFlags
   
   Public Shared Sub Main()
      Try
         Dim MyObject As New [MyClass]()
         Dim myMemberInfo() As MemberInfo
         
         ' Get the type of the class 'MyClass'.
         Dim myType As Type = MyObject.GetType()
         
         ' Get the public instance members of the class 'MyClass'. 
         myMemberInfo = myType.GetMembers((BindingFlags.Public Or BindingFlags.Instance))
         
         Console.WriteLine(ControlChars.Cr + "The public instance members of class '{0}' are : " + ControlChars.Cr, myType)
         Dim i As Integer
         For i = 0 To myMemberInfo.Length - 1
            ' Display name and type of the member of 'MyClass'.
            Console.WriteLine("'{0}' is a {1}", myMemberInfo(i).Name, myMemberInfo(i).MemberType)
         Next i
      
      Catch e As SecurityException
         Console.WriteLine(("SecurityException : " + e.Message.ToString()))
      End Try


      'Output:
      'The public instance members of class 'MyClass' are :

      ''Myfunction' is a Method
      ''ToString' is a Method
      ''Equals' is a Method
      ''GetHashCode' is a Method
      ''GetType' is a Method
      ''.ctor' is a Constructor
      ''myInt' is a Field
      ''myString' is a Field


   End Sub
End Class

Remarks

成員包括屬性、方法、函式、欄位、事件和巢狀型別。Members include properties, methods, constructors, fields, events, and nested types.

為了讓 GetMethods(BindingFlags) 多載成功地抓取方法資訊,bindingAttr 引數必須包含至少一個 BindingFlags.InstanceBindingFlags.Static,以及至少一個 BindingFlags.NonPublicBindingFlags.PublicFor the GetMethods(BindingFlags) overload to successfully retrieve method information, the bindingAttr argument must include at least one of BindingFlags.Instance and BindingFlags.Static, along with at least one of BindingFlags.NonPublic and BindingFlags.Public. 唯一的例外狀況是使用 BindingFlags.NonPublic的方法呼叫,它會傳回有關巢狀型別的成員資訊。The sole exception is a method call with BindingFlags.NonPublic, which returns member information about nested types.

下列 BindingFlags 篩選準則旗標可用來定義要包含在搜尋中的成員:The following BindingFlags filter flags can be used to define which members to include in the search:

  • 指定 BindingFlags.Instance 以包含實例方法。Specify BindingFlags.Instance to include instance methods.

  • 指定 BindingFlags.Static 以包含靜態方法。Specify BindingFlags.Static to include static methods.

  • 指定要在搜尋中包含公用方法 BindingFlags.PublicSpecify BindingFlags.Public to include public methods in the search.

  • 指定要在搜尋中包含非公用方法(也就是私用、內部和受保護的方法) BindingFlags.NonPublicSpecify BindingFlags.NonPublic to include non-public methods (that is, private, internal, and protected methods) in the search. 只會傳回基類上受保護和內部的方法;不會傳回基類上的私用方法。Only protected and internal methods on base classes are returned; private methods on base classes are not returned.

  • 指定 BindingFlags.FlattenHierarchy 在階層中包含 publicprotected 靜態成員;不包含繼承類別中 private 的靜態成員。Specify BindingFlags.FlattenHierarchy to include public and protected static members up the hierarchy; private static members in inherited classes are not included.

  • 單獨指定 BindingFlags.Default,以傳回空的 MethodInfo 陣列。Specify BindingFlags.Default alone to return an empty MethodInfo array.

下列 BindingFlags 的修飾詞旗標可以用來變更搜尋的運作方式:The following BindingFlags modifier flags can be used to change how the search works:

  • BindingFlags.DeclaredOnly 只搜尋 Type上宣告的成員,而不是直接繼承的成員。BindingFlags.DeclaredOnly to search only the members declared on the Type, not members that were simply inherited.

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

GetMembers 方法不會以特定順序傳回成員,例如依字母順序或宣告順序。The GetMembers method does not return members in a particular order, such as alphabetical or declaration order. 您的程式碼不能相依于傳回成員的順序,因為該順序會有所不同。Your code must not depend on the order in which members are returned, because that order varies.

若要使用這個方法多載來取得類別初始化運算式(靜態的函式) | ,您必須指定 BindingFlags.Static BindingFlags.NonPublic (在 BindingFlags.NonPublicBindingFlags.StaticOrVisual 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.

如果目前的 Type 代表結構化的泛型型別,這個方法會傳回具有型別參數的 MemberInfo 物件,並由適當的型別引數取代。If the current Type represents a constructed generic type, this method returns the MemberInfo objects with the type parameters replaced by the appropriate type arguments.

如果目前的 Type 表示泛型型別或泛型方法定義中的類型參數,則這個方法會搜尋類別條件約束的成員,或如果沒有類別條件約束,則為 Object 的成員。If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the members of the class constraint, or the members of Object if there is no class constraint.

See also

Applies to