12. ? 2020 株式会社ヘマタイト
Linq Expression
Func<int, int> f = i =>
{
int x = 0;
for (; ;)
{
x += i;
i -= 1;
if (i <= 0) break;
}
return x;
};
var x = Expression.Parameter(typeof(int), "x");
var i = Expression.Parameter(typeof(int), "i");
var endLoop = Expression.Label("EndLoop");
var body = Expression.Block(
typeof(int),
new[] { x },
Expression.Assign(x, Expression.Constant(0)),
Expression.Loop(
Expression.Block(
Expression.AddAssign(x, i),
Expression.SubtractAssign(i,
Expression.Constant(1)),
Expression.IfThen(
Expression.LessThan(i,
Expression.Constant(0)),
Expression.Break(endLoop))),
endLoop),
x);
var e = Expression.Lambda<Func<int, int>>(
body, i);
var f = e.Compile();
https://ufcpp.net/study/csharp/sp3_expression.html