Paulo Morgado

.NET Development & Architecture

This Blog

Syndication

Search

Tags

News

Unit Test Today! Get Typemock Isolator!

Projects

Books

 

Visitors

Visitor Locations

Community

Email Notifications

Archives

Profile

Disclaimer

The opinions and viewpoints expressed in this site are mine and do not necessarily reflect those of Microsoft, my employer or any community that I belong to. Any code or opinions are offered as is. Products or services mentioned are purchased by me, made available to me by my employer or the manufacturer/vendor which doesn't influence my opinion in any way.

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));
}

Published Fri, Mar 23 2007 13:06 by Paulo Morgado

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: