An expression tree may not contain a reference to a local function

痴心易碎 提交于 2019-12-04 03:51:13

Here is the pull request in Roslyn which makes this change:

References to local functions are now disallowed in expression trees, which may or may not change in the future (Previously they were generated as a reference to a mangled method name, which seemed wrong). Added a new error for this.

So the reasoning behind this is: when you reference a method in expression tree - it is represented as MethodCall expression with given method name. If you reference local function with name ImageFileProperties - you would expect MethodCall with the same name. Expression tree purpose is to be analyzed and deconstructed, so names are important there. But in reality local functions are compiled as static function with names like <Initialize>g__ImageFileProperties1_0 (what is referenced to as "mangled method name" in quotation above). For this reason Roslyn developer(s) decided to just not allow this to avoid confusion (name of the function you see in source code and name of the function in expression tree). With anonymous function there is no such confusion, so they are allowed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!