GetMembers Método
Collapse the table of content
Expand the table of content
Este artículo proviene de un motor de traducción automática. Mueva el puntero sobre las frases del artículo para ver el texto original. Más información.
Traducción
Original

Type.GetMembers (Método) ()

 

Devuelve todos los miembros públicos del objeto Type actual.

Espacio de nombres:   System
Ensamblado:  mscorlib (en mscorlib.dll)

public MemberInfo[] GetMembers()

Valor devuelto

Type: System.Reflection.MemberInfo[]

Matriz de objetos MemberInfo que representa todos los miembros públicos del objeto Type actual.

o bien

Matriz vacía de tipo MemberInfo si el objeto Type actual no tiene miembros públicos.

Implementaciones

_Type.GetMembers()

Members include properties, methods, fields, events, and so on.

The M:System.Type.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.

This method overload calls the M:System.Type.GetMembers(System.Reflection.BindingFlags) method overload, with F:System.Reflection.BindingFlags.Public | F:System.Reflection.BindingFlags.Instance | F:System.Reflection.BindingFlags.Static (F:System.Reflection.BindingFlags.PublicOrF:System.Reflection.BindingFlags.InstanceOrF:System.Reflection.BindingFlags.Static in Visual Basic). It will not find class initializers (.cctor). To find class initializers, use an overload that takes T:System.Reflection.BindingFlags, and specify F:System.Reflection.BindingFlags.Static | F:System.Reflection.BindingFlags.NonPublic (F:System.Reflection.BindingFlags.StaticOrF:System.Reflection.BindingFlags.NonPublic in Visual Basic). You can also get the class initializer using the P:System.Type.TypeInitializer property.

The following table shows what members of a base class are returned by the Get methods when reflecting on a type.

Member Type

Static

Non-Static

Constructor

No

No

Field

No

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. 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. 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. 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.

If the current T:System.Type represents a constructed generic type, this method returns the T:System.Reflection.MemberInfo objects with the type parameters replaced by the appropriate type arguments.

If the current T:System.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 T:System.Object if there is no class constraint.

The following code example demonstrates how to use the M:System.Type.GetMembers method overload to collect information about all public members of a specified class.

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 ); 
      }
   }
}

.NET Framework
Disponible desde 1.1
Portable Class Library
Compatible con: portable .NET platforms
Silverlight
Disponible desde 2.0
Windows Phone Silverlight
Disponible desde 7.0
Volver al principio
Mostrar:
© 2016 Microsoft