Type.GetMembers 方法

定義

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

多載

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

傳回

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.

實作

範例

下列程式碼範例將示範如何使用方法多載 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

備註

成員包括屬性、方法、函式、欄位、事件和巢狀型別。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.Static BindingFlags.Public Or BindingFlags.Instance Or BindingFlags.Static 在 Visual Basic) 中使用 | | (。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) ,並指定 BindingFlags.Static BindingFlags.NonPublic BindingFlags.Static Or BindingFlags.NonPublic Visual Basic) 中 | (。To find class initializers, call the GetMembers(BindingFlags) overload, and specify BindingFlags.Static | BindingFlags.NonPublic (BindingFlags.StaticOrBindingFlags.NonPublic in Visual Basic). 您也可以使用屬性取得類別初始化運算式 TypeInitializerYou can also get the class initializer using the TypeInitializer property.

下表顯示在反映型別時,方法會傳回基類的成員 GetThe 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. 依名稱和簽章會考慮簽章的所有部分,包括自訂修飾詞、傳回類型、參數類型、個 sentinel 和非受控呼叫慣例。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.

另請參閱

適用於

產品 Introduced

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

參數

bindingAttr
BindingFlags

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

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

傳回

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.

實作

範例

下列程式碼範例將示範如何使用方法多載 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

備註

成員包括屬性、方法、函式、欄位、事件和巢狀型別。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.Public 要在搜尋中包含公用方法。Specify BindingFlags.Public to include public methods in the search.

  • 指定 BindingFlags.NonPublic 要包含非公用方法 (也就是在搜尋中) 私用、內部和受保護的方法。Specify 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.FlattenHierarchypublic 在階層中包含和 protected 靜態成員; 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.Static Or 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). 您也可以使用屬性取得類別初始化運算式 TypeInitializerYou 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.

另請參閱

適用於

產品 Introduced