How to get a function’s MethodInfo from within the function?

Suppose there is a function GetEmployees:

public static List<Employee> GetEmployees(Dictionary<int, Department> depts, bool isFullTime)
{
    // How do I get the MethodInfo of this "GetEmployees" function by writing code here?
}

The reason I need to get the MethodInfo inside this function is that I need to know

  • The function name (“GetEmployees”)
  • The number of parameters (“2”)
  • The parameter names (“depts” and “isFullTime”)
  • The parameter types (“System.Collections.Generic.Dictionary`2[int, Department]&” and “System.Boolean”)
  • The return type

Thanks!

Answer

I expect you want this:

MethodInfo myMethod = MethodInfo.GetCurrentMethod();

And you can get the reflected parameters like so:

ParameterInfo[] parameters = myMethod.GetParameters();