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), /*...???... */);
Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
  • An expression does not have a method info. If you compile it, you can get the method info from the delegate. – leppie Jan 06 '15 at 06: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? – Water Cooler v2 Jan 06 '15 at 06:32

2 Answers2

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

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
leppie
  • 115,091
  • 17
  • 196
  • 297
  • 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? – Water Cooler v2 Jan 06 '15 at 06:59
  • 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. – leppie Jan 06 '15 at 07:22
  • Ah, now I see what you mean. You're saying if that `Func` already has other methods in its invocation list, then just be careful, right? – Water Cooler v2 Jan 06 '15 at 08:13
  • You can use a cast instead of `new`. – NetMage Mar 25 '19 at 17:39
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;
Timothy Shields
  • 75,459
  • 18
  • 120
  • 173
  • 2
    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. – Water Cooler v2 Jan 06 '15 at 06:18