考虑如下:
static int M() { Console.Write("M"); return 1; }
static int N() { Console.Write("N"); return 2; }
static int Q(int m, int n) { return m + n; }
...
Func<int> f = ()=>Q(n : N(), m: M());
Expression<Func<int>> x = ()=>Q(n : N(), m: M());
Func<int> fx = x.Compile();
Console.WriteLine(f());
Console.WriteLine(fx());
你们同意,我希望,最后两条路线必须完全相同,正确吗? 印刷NM3
。
现在,树木图书馆所说的话是什么,你希望树林的转化能够确保这一点? 无。 因此,我们面临以下选择:
- Implement the feature in the expression tree library. Add a transformation in the expression tree lowering engine that preserves the order of execution of the named arguments. Implement code in the
Compile
method that takes the execution order into account.
- Make
x = ()=>Q(n : N(), m: M());
actually be implemented as x = ()=>Q(M(), N());
and be incompatible with the non-expression-tree version.
- Disallow named arguments in expression trees. Implement an error message to that effect.
(1) 是冰,但代价高昂。 (2) 是非开端者;我们可以本着良知引入这种“cha”。 (3) 价格低廉,但令人不快。
我们选择了(3)。