Skip to content

Instantly share code, notes, and snippets.

@jeremiahredekop
Created July 4, 2012 20:50
Show Gist options
  • Save jeremiahredekop/3049473 to your computer and use it in GitHub Desktop.
Save jeremiahredekop/3049473 to your computer and use it in GitHub Desktop.
Get Method Info for Expression
public MethodInfo GetMethodInfo<TSource>(Expression<Action<TSource>> methodLambda)
{
Type type = typeof(TSource);
var member = methodLambda.Body as MethodCallExpression;
if (member == null)
throw new ArgumentException(string.Format(
"Expression '{0}' does not refer to a method.",
methodLambda));
var methodInfo = member.Method as MethodInfo;
if (methodInfo == null)
throw new ArgumentException(string.Format(
"Expression '{0}' does not refer to a method.",
methodLambda));
if (type != methodInfo.ReflectedType &&
!type.IsSubclassOf(methodInfo.ReflectedType))
throw new ArgumentException(string.Format(
"Expresion '{0}' refers to a method that is not from type {1}.",
methodLambda, type));
return methodInfo;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment