我知道可以使用MethodInfo.GetMethodBody().LocalVariables迭代方法中所有本地定义的变量。
但是如果我定义一个方法如下:
匿名方法可以作为闭包访问someint和test,但GetMethodBody().LocalVariables将为空。
有没有一种方法可以使用反射来检测闭包?
但是如果我定义一个方法如下:
public static void somemethod( int someint )
{
int test = 1;
HM.M(() =>
{
test = 2;
});
}
匿名方法可以作为闭包访问someint和test,但GetMethodBody().LocalVariables将为空。
有没有一种方法可以使用反射来检测闭包?
Program/'<>c__DisplayClass1'
,但是Reflection并没有提供这种工具。您可能可以使用Mono.Cecil或System.Reflection.Metadata来完成这个任务。 - Mike Zboray