Skip to content

Instantly share code, notes, and snippets.

@wonderful-panda
Created September 28, 2015 15:51
Show Gist options
  • Select an option

  • Save wonderful-panda/f80efc3a9e5164a35264 to your computer and use it in GitHub Desktop.

Select an option

Save wonderful-panda/f80efc3a9e5164a35264 to your computer and use it in GitHub Desktop.
Expression<Func<T, TRet>>に対する部分適用
void Main()
{
var x = 4;
var y = 2;
Expression<Func<int>> exp = () => x;
Expression<Func<int, int>> twice = n => n * y;
var param = twice.Parameters[0];
var exp2 = new Visitor(exp.Body, twice.Body).Visit(exp);
var exp3 = (Expression<Func<int>>)new Visitor(param, exp.Body).Visit(exp2);
var f = exp3.Compile();
Console.WriteLine(f());
x = 5;
Console.WriteLine(f());
}
// Define other methods and classes here
class Visitor : ExpressionVisitor
{
Expression _old;
Expression _new;
public Visitor(Expression oldNode, Expression newNode)
{
_old = oldNode;
_new = newNode;
}
public override Expression Visit(Expression node)
{
if (node == _old)
return _new;
return base.Visit(node);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment