// File: SymbolExtensions.cs // Author: Samuel Jack // Article: "Getting the MethodInfo of a generic method using Lambda expressions" // URL: http://blog.functionalfun.net/2009/10/getting-methodinfo-of-generic-method.html using System.Linq.Expressions; using System.Reflection; using System; namespace PixelCrushers.DialogueSystem { // Put in DialogueSystem namespace to prevent conflicts. public static class SymbolExtensions { /// /// Given a lambda expression that calls a method, returns the method info. /// /// /// The expression. /// public static MethodInfo GetMethodInfo(Expression expression) { return GetMethodInfo((LambdaExpression)expression); } /// /// Given a lambda expression that calls a method, returns the method info. /// /// /// The expression. /// public static MethodInfo GetMethodInfo(Expression> expression) { return GetMethodInfo((LambdaExpression)expression); } /// /// Given a lambda expression that calls a method, returns the method info. /// /// /// The expression. /// public static MethodInfo GetMethodInfo(Expression> expression) { return GetMethodInfo((LambdaExpression)expression); } /// /// Given a lambda expression that calls a method, returns the method info. /// /// The expression. /// public static MethodInfo GetMethodInfo(LambdaExpression expression) { MethodCallExpression outermostExpression = expression.Body as MethodCallExpression; if (outermostExpression == null) { throw new ArgumentException("Invalid Expression. Expression should consist of a Method call only."); } return outermostExpression.Method; } } }