How to get only the Overridden methods of a base class using reflection

Posted by Rajnilari2015 under C# category on | Points: 40 | Views : 795
Suppose we have a class as under

public class Employee
public int EmployeeId { get; set; }
public void Display()
Console.WriteLine("{0} {1}", EmployeeId, EmployeeName);
public override string ToString()
return String.Format("Overriden Method : {0} {1}", EmployeeId, EmployeeName);

The intention is to obtain only the override method of the child class i.e. ToString(). The below is the way to do that

BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.Static |
.Where(m => m.IsVirtual )
.ForEach(i => Console.WriteLine(i.Name));

The IsVirtual property of MethodBase class does the trick.This property Gets a value indicating whether the method is virtual.It returns true if this method is virtual; otherwise, false. It is defined as
public bool IsVirtual { get; }

Comments or Responses

Login to post response