Guest User

Untitled

a guest
Dec 11th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.56 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6.  
  7. namespace ConsoleApplication1 {
  8.  
  9.     public class Koos {
  10.         public string name { get; set; }
  11.     }
  12.     public class Jan {
  13.         public string Name { get; set; }
  14.     }
  15.  
  16.     public class TransformModifier : ExpressionVisitor {
  17.         ParameterExpression param; // this stops the class from being thread-safe!
  18.  
  19.         public Expression Modify(Expression expression) {
  20.             return Visit(expression);
  21.         }
  22.  
  23.         protected override Expression VisitMember(MemberExpression node) {
  24.             if (node.Expression.Type == typeof(Jan)) {
  25.                 // redirect! 'param' was assigned previously, in VisitLambda
  26.                 var redir = Expression.MakeMemberAccess(param, typeof(Koos).GetMember("name")[0]);
  27.                 return redir;
  28.             } else return base.VisitMember(node);
  29.         }
  30.  
  31.         protected override Expression VisitLambda<T>(Expression<T> node) {
  32.             // convert "Jan" parameter to "Koos" parameter.
  33.             // don't need to type-check; static type-checking should take care of this for me?
  34.             param = Expression.Parameter(typeof(Koos));
  35.             return Expression.Lambda<Func<Koos, bool>>(Visit(node.Body), param);
  36.         }
  37.  
  38.         protected override Expression VisitBinary(BinaryExpression node) {
  39.             return base.VisitBinary(Expression.MakeBinary(node.NodeType, Visit(node.Left), Visit(node.Right)));
  40.         }
  41.     }
  42.  
  43.     public class Program {
  44.  
  45.         static void Main(string[] args) {
  46.             Expression<Func<Jan, bool>> exp = j => j.Name == "Jan";
  47.             var janCompiled = exp.Compile();
  48.             Console.WriteLine("Expect this to be false: {0}", janCompiled(new Jan() { Name = "Bob" }));
  49.             Console.WriteLine("Expect this to be true: {0}", janCompiled(new Jan() { Name = "Jan" }));
  50.             Console.WriteLine(exp);
  51.             foreach (var p in exp.Parameters) Console.WriteLine(p.Type);
  52.             var t = new TransformModifier();
  53.             var mod = (Expression<Func<Koos,bool>>)t.Modify(exp);
  54.             Console.WriteLine("Modified:");
  55.             foreach (var p in mod.Parameters) Console.WriteLine(p.Type);
  56.             Console.WriteLine(mod);
  57.             var compiled = mod.Compile();
  58.             Console.WriteLine("Expect this to be false: {0}", compiled(new Koos() { name = "Bob" }));
  59.             Console.WriteLine("Expect this to be true: {0}", compiled(new Koos() { name = "Jan" }));
  60.             Console.ReadKey();
  61.         }
  62.     }
  63. }
Add Comment
Please, Sign In to add comment