Created
September 28, 2015 15:51
-
-
Save wonderful-panda/f80efc3a9e5164a35264 to your computer and use it in GitHub Desktop.
Expression<Func<T, TRet>>に対する部分適用
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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