反思 - 获取里面的lambda EX pression的方法调用列表里面、方法、列表、EX

2023-09-03 03:47:06 作者:你没心没肺我撕心裂肺

我试图找到一种方式来获得列表的方式调用内部的lambda EX pression在C#3.5。例如,在下面的code,我想方法LookAtThis(行动)来分析的拉姆达EX pression内容。换句话说,我想LookAtThis回到我创建的MethodInfo的对象。

I am trying to find a way to get the list of method calls inside a lambda expression in C# 3.5. For instance, in the code below, I would like to method LookAtThis(Action a) to analyze the content of the lambda expression. In other words, I want LookAtThis to return me the MethodInfo object of Create.

LookAtThis(() => Create(null, 0));

这可能吗?

谢谢!

推荐答案

这是相当容易的,只要你使用防爆pression<作用> 而不是动作。对于全code,包括如何获得实际值​​暗示,see这里 - 尤其是 ResolveMethod (以及它是如何使用调用)。这是code我使用protobuf网基于lambda表达式做RPC。

This is fairly easy as long as you use Expression<Action> instead of Action. For full code, including how to get the actual values implied, see here - in particular ResolveMethod (and how it is used by Invoke). This is the code I use in protobuf-net to do RPC based on lambdas.