9

I know I am asking the bizarre but just for kicks, is it possible to get the MethodInfo for a lambda expression?

I am after something like this:

(Func<int, string>(i => i.ToString())).MethodInfo

UPDATE I want to get the method info regardless of whether the body of the lamda is a method call expression or not, i.e. regardless of what type of expression the body of the lambda is.

So, for e.g.

This might work.

var intExpression = Expression.Constant(2); Expression<Func<int, Dog>> conversionExpression = i => Program.GetNewDog(i); var convertExpression5 = Expression.ConvertChecked(intExpression, typeof(Dog), ((MethodCallExpression)(conversionExpression.Body)).Method); class Program { static Dog GetNewDog(int i) { return new Dog(); } } 

But I want even this to work:

var intExpression = Expression.Constant(2); Expression<Func<int, Dog>> conversionExpression = i => new Dog(); var convertExpression5 = Expression.ConvertChecked(intExpression, typeof(Dog), /*...???... */); 
2
  • An expression does not have a method info. If you compile it, you can get the method info from the delegate. Commented Jan 6, 2015 at 6:32
  • I am not interested in creating an expression. I added the update in response to Timothy Shields' answer below. All I want is a methodInfo from a delegate. Possible? Commented Jan 6, 2015 at 6:32

2 Answers 2

15

You are quite close :)

You can do something like this:

MethodInfo meth = (new Func<int, string>(i => i.ToString())).Method; 

Note: This might have problems if you have multiple 'subscribers' to a delegate instance.

Reference: https://learn.microsoft.com/en-us/dotnet/api/system.delegate.method

Sign up to request clarification or add additional context in comments.

5 Comments

But how could there be multiple subscribers to an anonymous method or a lambda? How would you even reference it at any place other than where it is declared?
You would have to first declare the delegate and then use += to add additional subscribers. I agree, with anonymous delegates, this scenario is very unlikely. You can check for it though, or Method might be null in that case. Also be aware the delegate might have some context if the Target property is not null.
Ah, now I see what you mean. You're saying if that Func<T> already has other methods in its invocation list, then just be careful, right?
You can use a cast instead of new.
This answer is no longer true, in recent .NET editions, C# creates a private class to prevent accessing private methods used in delegates via reflection. dotnetfiddle.net/8vfc8A , if you run this fiddle, you will find that method name printed is not ToString but some random generated name. You have to use Expressions to get correct MethodInfo.
11

Using the System.Linq.Expressions namespace, you can do the following.

Expression<Func<int, string>> expression = i => i.ToString(); MethodInfo method = ((MethodCallExpression)expression.Body).Method; 

1 Comment

No, wait. That'll only work for this particular example. My bad. I should've clarified. I want it to work regardless of what the body of the lambda contains. This solution assumes that the body of the lambda is a method call expression, which is my fault, because I gave such an example.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.