Getting MethodInfo using LINQ

At the last MVP Global Summit, Mads showed me and Rich a way to get the MethodInfo of a method in a strongly typed way using LINQ. Here is how you can do it:

public static class TypeHelper
{
    public delegate void Method();
 
    public delegate void Method<TArg0>(TArg0 arg0);
 
    public delegate void Method<TArg0, TArg1>(TArg0 arg0, TArg1 arg1);
 
    public delegate void Method<TArg0, TArg1, TArg2>(TArg0 arg0, TArg1 arg1, TArg2 arg2);
 
    public delegate void Method<TArg0, TArg1, TArg2, TArg3>(TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3);
 
    public static System.Reflection.MethodInfo MethodInfoOf(System.Linq.Expressions.Expression<Method> expression)
    {
        return MethodInfoOfInternal(expression.Body as System.Linq.Expressions.MethodCallExpression);
    }
 
    public static System.Reflection.MethodInfo MethodInfoOf<TArg0>(System.Linq.Expressions.Expression<Method<TArg0>> expression)
    {
        return MethodInfoOfInternal(expression.Body as System.Linq.Expressions.MethodCallExpression);
    }
 
    public static System.Reflection.MethodInfo MethodInfoOf<TArg0, TArg1>(System.Linq.Expressions.Expression<Method<TArg0, TArg1>> expression)
    {
        return MethodInfoOfInternal(expression.Body as System.Linq.Expressions.MethodCallExpression);
    }
 
    public static System.Reflection.MethodInfo MethodInfoOf<TArg0, TArg1, TArg2>(System.Linq.Expressions.Expression<Method<TArg0, TArg1, TArg2>> expression)
    {
        return MethodInfoOfInternal(expression.Body as System.Linq.Expressions.MethodCallExpression);
    }
 
    public static System.Reflection.MethodInfo MethodInfoOf<TArg0, TArg1, TArg2, TArg3>(System.Linq.Expressions.Expression<Method<TArg0, TArg1, TArg2, TArg3>> expression)
    {
        return MethodInfoOfInternal(expression.Body as System.Linq.Expressions.MethodCallExpression);
    }
 
    private static System.Reflection.MethodInfo MethodInfoOfInternal(System.Linq.Expressions.MethodCallExpression methodCallExpression)
    {
        return (methodCallExpression == null) ? null : methodCallExpression.Method;
    }
}

Usage is also very simple:

static bool F(string s)
{
    return !string.IsNullOrEmpty(s);
}
 
static void M1()
{
}
 
static void M2(string s)
{
}
 
static void M3(string s1, string s2)
{
}
 
static void Main(string[] args)
{
    MethodInfo fi = TypeHelper.MethodInfoOf((string s) => F(s));
    MethodInfo mi1 = TypeHelper.MethodInfoOf((string s) => M1());
    MethodInfo mi2 = TypeHelper.MethodInfoOf((string s) => M2(s));
    MethodInfo mi3 = TypeHelper.MethodInfoOf((string s1, string s2) => M3(s1, s2));
}