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
typeof(Employee)
.GetMethods(
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.Static |
BindingFlags.DeclaredOnly
)
.Where(m => m.IsVirtual )
.ToList()
.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; }