Get the members of a type C# with Example



Get the members of a type C# with Example

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."); 
} 
} 
} 
Output (see note about output order further down): 
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. 
We can also use the GetMembers() without passing any BindingFlags. This will return all public members of that 
specific type. 
One thing to note that GetMembers does not return the members in any particular order, so never rely on the order 
that GetMembers returns you. 
View Demo 

0 Comment's

Comment Form