C# Language Obtener los miembros de un tipo


Ejemplo

using System;
using System.Reflection;
using System.Linq;
                
public class Program
{
  public static void Main()
  {
    var members = typeof(object)
                    .GetMembers(BindingFlags.Public |
                                BindingFlags.Static |
                                BindingFlags.Instance);
    
    foreach (var member in members)
    {
      bool inherited = member.DeclaringType.Equals( typeof(object).Name );
      Console.WriteLine($"{member.Name} is a {member.MemberType}, " +
                        $"it has {(inherited ? "":"not")} been inherited.");
    }
  }
}

Salida ( ver nota sobre el orden de salida más abajo ):

GetType is a Method, it has not been inherited.
GetHashCode is a Method, it has not been inherited.
ToString is a Method, it has not been inherited.
Equals is a Method, it has not been inherited.
Equals is a Method, it has not been inherited.
ReferenceEquals is a Method, it has not been inherited.
.ctor is a Constructor, it has not been inherited.

También podemos usar GetMembers() sin pasar BindingFlags . Esto devolverá a todos los miembros públicos de ese tipo específico.

Una cosa a tener en cuenta es que GetMembers no devuelve a los miembros en ningún orden en particular, por lo que nunca confíe en el pedido que GetMembers devuelve GetMembers .

Ver demostración