Skip to content

Instantly share code, notes, and snippets.

@filipw
Last active December 11, 2015 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filipw/4527112 to your computer and use it in GitHub Desktop.
Save filipw/4527112 to your computer and use it in GitHub Desktop.
public static MethodInfo GetMethodInfo<T, U>(Expression<Func<T, U>> expression)
{
var method = expression.Body as MethodCallExpression;
if (method != null)
return method.Method;
throw new ArgumentException("Expression is wrong");
}
public static MethodInfo GetMethodInfo<T, U>(Expression<Action<T>> expression)
{
var method = expression.Body as MethodCallExpression;
if (method != null)
return method.Method;
throw new ArgumentException("Expression is wrong");
}
//Then in the route tester you can add
public bool CompareSignatures(MethodInfo method)
{
if (_controllerContext.ControllerDescriptor == null)
GetControllerType();
var actionSelector = new ApiControllerActionSelector();
var x = actionSelector.GetActionMapping(_controllerContext.ControllerDescriptor)[_request.Method.ToString()];
return x.Any(item => ((MethodBase)(((ReflectedHttpActionDescriptor)item).MethodInfo)).ToString() == ((MethodBase)method).ToString());
}
//usage
Assert.IsTrue(routeTester.CompareSignatures(ReflectionHelpers.GetMethodInfo((PersonController p) => p.Get())));
Assert.IsTrue(routeTester.CompareSignatures(ReflectionHelpers.GetMethodInfo((PersonController p) => p.Get(new Guid("d39a7401-5ec7-4611-9934-fdcc1be0dc98")))));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment