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.StaticBindingFlags.Instance一个 ,以及至少一个 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.PublicBindingFlags.InstanceBindingFlags.Static | | Visual Basic) 中 (。BindingFlags.PublicOrBindingFlags.InstanceOrBindingFlags.Static 它找不到类初始值设定项 (静态构造函数) 。 若要查找类初始值设定项,请GetMembers(BindingFlags)调用 重载,并在 Visual Basic) BindingFlags.StaticBindingFlags.NonPublicOr中指定 BindingFlags.Static | BindingFlags.NonPublic (。 还可以使用 TypeInitializer 属性获取类初始值设定项。

下表显示了在对类型进行反射时方法返回 Get 的基类成员。

成员类型 静态 非静态
构造函数
字段 可以。 字段始终按名称和签名隐藏。
事件 不适用 常见类型系统规则是继承与实现 属性的方法的继承相同。 反射将属性视为按名称和签名隐藏。 请参阅下面的注释 2。
方法 可以。 (虚拟和非虚拟) 的方法可以是按名称隐藏或按名称和签名隐藏。
嵌套类型
properties 不适用 常见类型系统规则是继承与实现 属性的方法的继承相同。 反射将属性视为按名称和签名隐藏。 请参阅下面的注释 2。
  1. 按名称和签名隐藏考虑签名的所有部分,包括自定义修饰符、返回类型、参数类型、sentinels 和非托管调用约定。 这是二进制比较。

  2. 对于反射,属性和事件按名称和签名隐藏。 如果在基类中具有同时具有 get 和 set 访问器的属性,但派生类只有 get 访问器,则派生类属性将隐藏基类属性,并且您将无法访问基类上的 setter。

  3. 自定义属性不是通用类型系统的一部分。

如果当前 Type 表示构造的泛型类型,则此方法返回 MemberInfo 由相应类型参数替换的类型参数的对象。

如果当前 Type 表示泛型类型或泛型方法定义中的类型参数,则此方法搜索类约束的成员,如果没有类约束,则搜索 的成员 Object

另请参阅

适用于