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 'New
   
   Public Sub Myfunction()
   End Sub 'Myfunction
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 'Main
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.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). 此外可以获取类初始值设定项使用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. 按名称和签名隐藏会考虑所有的签名,包括自定义修饰符,部分返回类型、 参数类型、 标记和非托管调用约定。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 'New
   
   Public Sub Myfunction()
   End Sub 'Myfunction
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 'Main 
End Class 'Type_GetMembers_BindingFlags

注解

成员包括属性、 方法、 构造函数、 字段、 事件和嵌套的类型。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.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.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). 此外可以获取类初始值设定项使用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.

另请参阅

适用于