Turn on Option Strict, your code won't compile :)
Anyway, as far as I can see a
MethodCallExpression[
^] has an
Object Property[
^] and a
Method Property[
^], which in this case should probably hold the information you need.
Consider the following (which isn't safe to use with other lambda's):
Option Strict On
Module Module1
Sub Main()
Console.WriteLine(HelperFunction(Function() "test".ToString))
Console.WriteLine(HelperFunction(Function() "test".GetType))
Console.ReadKey()
End Sub
Public Function HelperFunction(Of T)(ByVal exp As Expressions.Expression(Of Func(Of T))) As String
Dim methodCall As Expressions.MethodCallExpression = DirectCast(exp.Body, Expressions.MethodCallExpression)
Dim obj As Expressions.ConstantExpression = DirectCast(methodCall.Object, Expressions.ConstantExpression)
Dim method As System.Reflection.MethodInfo = methodCall.Method
Return String.Format("The {0} method was called on the object '{1}' of type {2}.", method.Name, obj.Value, obj.Type.FullName)
End Function
End Module
This piece of code only works in your specific case. Try passing in
Integer.MaxValue
as a lambda and the casts will go very wrong because the
exp.Body
will now return a
ConstantExpression[
^].
I started dabbling into
ExpressionTrees[
^] just last week. Which helped me was
this article on MSDN[
^]. I recommend to click on some of the links on that page also.
As I only started with this last week I'm afraid I can't say much more on the topic. I hope this will help you on your way though. Good luck! :)