using System.Linq.Expressions;
using System.Reflection;
using System;
namespace Harmony
{
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)
{
var outermostExpression = expression.Body as MethodCallExpression;
if (outermostExpression == null)
throw new ArgumentException("Invalid Expression. Expression should consist of a Method call only.");
var method = outermostExpression.Method;
if (method == null)
throw new Exception("Cannot find method for expression " + expression);
return method;
}
}
}