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

Posted by Rajnilari2015 under VB.NET category on | Points: 40 | Views : 383
Suppose we have a class as under

Public Class Employee
Public Property EmployeeId() As Integer
Return m_EmployeeId
End Get
m_EmployeeId = Value
End Set
End Property
Private m_EmployeeId As Integer
Public Sub Display()
Console.WriteLine("{0} {1}", EmployeeId, EmployeeName)
End Sub
Public Overrides Function ToString() As String
Return [String].Format("Overriden Method : {0} {1}", EmployeeId, EmployeeName)
End Function
End Class

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

GetType(Employee).GetMethods(BindingFlags.[Public] Or BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.[Static] Or BindingFlags.DeclaredOnly).Where(Function(m) m.IsVirtual).ToList().ForEach(Function(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.

Comments or Responses

Login to post response