Type.GetMembers 方法

定義

取得目前 Type 的成員 (屬性、方法、欄位、事件等等)。

多載

GetMembers(BindingFlags)

在衍生類別中覆寫時,使用指定的繫結條件約束,搜尋定義給目前 Type 的成員。

GetMembers()

傳回目前 Type 的所有公用成員。

GetMembers(BindingFlags)

在衍生類別中覆寫時,使用指定的繫結條件約束,搜尋定義給目前 Type 的成員。

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

列舉值的位元組合,用來指定搜尋的執行方式。

-或-

要傳回空陣列的 Default

傳回

MemberInfo 物件的陣列,代表為目前 Type 定義之符合指定繫結條件約束的所有成員。

-或-

如果未針對目前的 Type 定義任何成員,或沒有已定義的成員符合繫結條件約束,則為空陣列。

實作

範例

下列程式代碼範例示範如何使用 GetMembers(BindingFlags) 方法多載來收集指定類別之所有公用實例成員的相關信息。

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
   }
}
open System.Reflection
open System.Security

type MyClass =
    val public myInt: int
    val public myString: string

    new () = { myInt = 0; myString = null}

    member _.MyMethod() = ()

try
    let MyObject = MyClass()

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

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

    printfn $"\nThe public instance members of class '{myType}' are : \n"
    for i = 0 to myMemberInfo.Length - 1 do
        // Display name and type of the member of 'MyClass'.
        printfn $"'{myMemberInfo[i].Name}' is a {myMemberInfo[i].MemberType}"
with :? SecurityException as e ->
    printfn $"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

備註

成員包括屬性、方法、建構函式、欄位、事件和巢狀類型。

GetMethods(BindingFlags)若要讓多載成功擷取方法資訊,自bindingAttr變數必須包含至少一個 BindingFlags.InstanceBindingFlags.Static,以及至少一個 BindingFlags.NonPublicBindingFlags.Public。 唯一的例外狀況是使用 BindingFlags.NonPublic的方法呼叫,它會傳回巢狀類型的成員資訊。

下列 BindingFlags 篩選旗標可用來定義要包含在搜尋中的成員:

  • 指定 BindingFlags.Instance 以包含實例方法。

  • 指定 BindingFlags.Static 以包含靜態方法。

  • 指定 BindingFlags.Public 以在搜尋中包含公用方法。

  • 指定 BindingFlags.NonPublic 以在搜尋中包含非公用方法 (,也就是私人、內部和受保護的方法) 。 只會傳回基類上的受保護和內部方法;不會傳回基類上的私用方法。

  • 指定要 BindingFlags.FlattenHierarchy 在階層中加入 publicprotected 靜態成員; private 繼承類別中的靜態成員不包含在內。

  • 單獨指定 BindingFlags.Default 以傳回空 MethodInfo 陣列。

下列 BindingFlags 修飾詞旗標可用來變更搜尋的運作方式:

  • BindingFlags.DeclaredOnly 只搜尋 在上 Type宣告的成員,而不是只繼承的成員。

如需相關資訊,請參閱 System.Reflection.BindingFlags

在 .NET 6 和舊版中 GetMembers ,方法不會以特定順序傳回成員,例如字母順序或宣告順序。 您的程式代碼不得取決於傳回成員的順序,因為該順序會有所不同。 不過,從 .NET 7 開始,排序會根據元件中的元數據排序來確定。

若要使用此方法多載取得類別初始化運算式 (靜態建構函式) ,您必須在 Visual Basic) 中指定 BindingFlags.Static | BindingFlags.NonPublic (。BindingFlags.StaticOrBindingFlags.NonPublic 您也可以使用 TypeInitializer 屬性取得類別初始化運算式。

如果目前 Type 代表建構的泛型型別,這個方法會傳回 MemberInfo 物件,其類型參數會由適當的型別自變數取代。

如果目前的 Type 代表泛型型別或泛型方法定義中的型別參數,這個方法會搜尋類別條件約束的成員,如果沒有類別條件約束,則為的成員 Object

另請參閱

適用於

GetMembers()

傳回目前 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 物件的陣列,代表目前 Type 的所有公用成員。

-或-

MemberInfo 類型的空陣列 (如果目前 Type 沒有公用成員)。

實作

範例

下列程式代碼範例示範如何使用 GetMembers() 方法多載來收集指定類別之所有公用成員的相關信息。

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 );
      }
   }
}
type MyClass =
    val public myInt: int
    val public myString: string

    new () = { myInt = 0; myString = null}

    member _.MyMethod() = ()

try
    let myObject = MyClass()

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

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

    printfn $"\nThe members of class '{myType}' are :\n"
    for i = 0 to myMemberInfo.Length - 1 do
    // Display name and type of the concerned member.
        printfn $"'{myMemberInfo[i].Name}' is a {myMemberInfo[i].MemberType}"
with e ->
    printfn $"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

備註

成員包括屬性、方法、建構函式、欄位、事件和巢狀類型。

在 .NET 6 和舊版中 GetMembers ,方法不會以特定順序傳回成員,例如字母順序或宣告順序。 您的程式代碼不得取決於傳回成員的順序,因為該順序會有所不同。 不過,從 .NET 7 開始,排序會根據元件中的元數據排序來確定。

這個方法多載會GetMembers(BindingFlags)呼叫 方法多載,在BindingFlags.InstanceBindingFlags.StaticBindingFlags.Public | | Visual Basic中 (OrBindingFlags.StaticBindingFlags.PublicOrBindingFlags.Instance) 。 它找不到靜態建構函式 (類別初始化表達式) 。 若要尋找類別初始化表達式,請呼叫 GetMembers(BindingFlags) 多載,並在Visual Basic) 中指定BindingFlags.Static | BindingFlags.NonPublic (。BindingFlags.StaticOrBindingFlags.NonPublic 您也可以使用 TypeInitializer 屬性取得類別初始化運算式。

下表顯示反映類型時,方法會傳 Get 回基類的成員。

成員類型 Static 非靜態
建構函式
欄位 可以。 欄位一律會依名稱與簽章隱藏。
事件 不適用 常見的類型系統規則是繼承與實作 屬性的方法相同。 反映會將屬性視為依名稱與簽章隱藏。 請參閱下面的附註 2。
方法 可以。 (虛擬和非虛擬) 的方法可以是依名稱隱藏或依名稱隱藏和簽章。
巢狀類型
屬性 不適用 常見的類型系統規則是繼承與實作 屬性的方法相同。 反映會將屬性視為依名稱與簽章隱藏。 請參閱下面的附註 2。
  1. 隱藏名稱與簽章會考慮簽章的所有部分,包括自定義修飾詞、傳回型別、參數類型、sentinels 和 Unmanaged 呼叫慣例。 這是二進位比較。

  2. 對於反映,屬性和事件會依名稱與簽章隱藏。 如果您的屬性同時具有基類中的 get 和 set 存取子,但衍生類別只有 get 存取子,則衍生類別屬性會隱藏基類屬性,而且您將無法存取基類上的 setter。

  3. 自訂屬性不是通用類型系統的一部分。

如果目前 Type 代表建構的泛型型別,這個方法會傳回 MemberInfo 物件,其類型參數會由適當的型別自變數取代。

如果目前的 Type 代表泛型型別或泛型方法定義中的型別參數,這個方法會搜尋類別條件約束的成員,如果沒有類別條件約束,則為的成員 Object

另請參閱

適用於