Type.GetMembers Type.GetMembers Type.GetMembers Type.GetMembers Method

定义

获取当前 Type 的成员(包括属性、方法、字段、事件等)。Gets the members (properties, methods, fields, events, and so on) of the current Type.

重载

GetMembers() GetMembers() GetMembers() GetMembers()

返回为当前 Type 的所有公共成员。Returns all the public members of the current Type.

GetMembers(BindingFlags) GetMembers(BindingFlags) GetMembers(BindingFlags) GetMembers(BindingFlags)

当在派生类中重写时,使用指定绑定约束,搜索为当前 Type 定义的成员。When overridden in a derived class, searches for the members defined for the current Type, using the specified binding constraints.

GetMembers() GetMembers() GetMembers() GetMembers()

返回为当前 Type 的所有公共成员。Returns all the public members of the current Type.

public:
 virtual cli::array <System::Reflection::MemberInfo ^> ^ GetMembers();
public System.Reflection.MemberInfo[] GetMembers ();
abstract member GetMembers : unit -> System.Reflection.MemberInfo[]
override this.GetMembers : unit -> System.Reflection.MemberInfo[]
Public Function GetMembers () As 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 '[MyClass]

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 'Type_GetMembers

注解

成员包括属性、方法、构造函数、字段、事件和嵌套类型。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.StaticBindingFlags.Instance在 Visual Basic 中)。BindingFlags.Public BindingFlags.Public | BindingFlags.Instance | Or Or BindingFlags.StaticThis 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.StaticBindingFlags.Static并| BindingFlags.NonPublic指定(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). 还可以使用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 NoNo NoNo
字段Field NoNo 可以。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 NoNo 可以。Yes. 方法(虚拟和非虚拟)可按名称隐藏或按名称和签名隐藏。A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature.
嵌套类型Nested Type NoNo NoNo
PropertyProperty 不适用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 访问器,则派生类属性将隐藏基类属性,并且你将无法访问基类的资源库。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.

另请参阅

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

枚举值的按位组合,这些值指定如何进行搜索。A bitwise combination of the enumeration values that specify how the search is conducted.

- 或 --or- 若为 Default,则返回空数组。Default to return an empty array.

返回

表示为当前 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 '[MyClass]

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 'Type_GetMembers_BindingFlags

注解

成员包括属性、方法、构造函数、字段、事件和嵌套类型。Members include properties, methods, constructors, fields, events, and nested types.

BindingFlags.NonPublic BindingFlags.Instance BindingFlags.Static BindingFlags.Public为了使bindingAttr重载成功检索方法信息,参数必须包含至少一个和中的至少一个和和。 GetMethods(BindingFlags)For 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.FlattenHierarchy在层次public结构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.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.

如果当前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.

另请参阅

适用于