Created
July 4, 2012 20:50
-
-
Save jeremiahredekop/3049473 to your computer and use it in GitHub Desktop.
Get Method Info for Expression
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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