使用LINQ表达式树解析方法调用 [英] Parsing a Method Call Using LINQ Expression Trees

查看:50
本文介绍了使用LINQ表达式树解析方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解析/分解表达式树,这是我的第一步,但我很难弄清楚如何准确地访问函数中的变量和方法调用.说我有这个辅助功能:

I am taking my first baby steps with parsing/decomposing expression trees and I''m having trouble figuring out how exactly to get access to the variable and method call in a function. Say I have this helper function:

Public Function HelperFunction(Of T)(ByVal exp As Expressions.Expression(Of Func(Of T))) As String
    Dim mCall As Expressions.MethodCallExpression = exp.Body
    Return mCall.ToString()
End Function


我这样称呼:


And I call it in this manner:

MessageBox.Show(HelperFunction(Function() "test".ToString()))


将显示以下内容:"test" .ToString()

我想做的是获得对实例变量("test")和方法("ToString")的访问权限,然后使用反射在该实例变量上调用该方法.我非常有信心可以弄清楚如何使用反射进行方法调用,但是我不确定如何使用表达式树代码来获取实例变量和所调用的方法.


The following will be shown: "test".ToString()

What I would like to do is gain access to the instance variable ("test") and the method ("ToString") and then use reflection to call that method on that instance variable. I am pretty confident I can figure out how to use reflection to make the method call, but I''m not sure how to use the expression tree code to get the instance variable and the method called.

推荐答案

打开Option Strict,您的代码将无法编译:)
无论如何,据我所见, MethodCallExpression [ ^ ]具有对象属性 [方法属性 [
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 ' Really!

Module Module1

    Sub Main()
        Console.WriteLine(HelperFunction(Function() "test".ToString))
        ' The ToString method was called on the object 'test' of type System.String.
        Console.WriteLine(HelperFunction(Function() "test".GetType))
        ' The GetType method was called on the object 'test' of type System.String.
        'Console.WriteLine(HelperFunction(Function() Integer.MaxValue))
        ' Produces an Exception! The cast to MethodCallExpression will fail.
        Console.ReadKey()
    End Sub
    
    Public Function HelperFunction(Of T)(ByVal exp As Expressions.Expression(Of Func(Of T))) As String
        ' Now with Option Strict On we have to cast this :) <- smilies are a hidden VB feature! ;p
        Dim methodCall As Expressions.MethodCallExpression = DirectCast(exp.Body, Expressions.MethodCallExpression)
        ' Gets the object on which the method was called, which is in this case represented in a ConstantExpression.
        Dim obj As Expressions.ConstantExpression = DirectCast(methodCall.Object, Expressions.ConstantExpression)
        ' Gets the method that was called. Returns a System.Reflection.MethodInfo.
        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

这段代码仅适用于您的特定情况.尝试将Integer.MaxValue作为lambda传入,由于exp.Body现在将返回 ^ ].
我开始涉足 ExpressionTrees [有关MSDN的这篇文章 [

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! :)


最简单的是:
The simplest would be:
var res = exp.Compile()();


无需明确地进行任何反射.


如果要以特定方式分析表达式树,则可能必须编写特定的访问者才能遍历表达式树.请参阅
如何:实现表达式树访问者 [


No need to do any reflection explicitly.


If you want to analyze the expression tree in a specific way, you might have to write your specific visitor to traverse the expression tree. See How to: Implement an Expression Tree Visitor[^]
[END EDIT]

Cheers

Andi


这篇关于使用LINQ表达式树解析方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆