Created
May 21, 2013 10:03
-
-
Save daanl/5618733 to your computer and use it in GitHub Desktop.
Expressions
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
using System; | |
using System.Linq.Expressions; | |
using NUnit.Framework; | |
namespace ClassLibrary1 | |
{ | |
[TestFixture] | |
public class Class2 | |
{ | |
[Test] | |
public void CanReadExpression() | |
{ | |
var filter = new Filter(); | |
filter.Run(x => x.Body.Equals("test")); | |
filter.Run(x => !x.Body.Equals("test")); | |
filter.Run(x => x.Body == "test"); | |
} | |
} | |
public class Filter | |
{ | |
public void Run(Expression<Func<Message, bool>> expression) | |
{ | |
var body = expression.Body; | |
if (body is MethodCallExpression) | |
{ | |
var methodCallExpression = body as MethodCallExpression; | |
var method = methodCallExpression.Method; | |
Console.WriteLine("Method name {0}", method.Name); | |
} | |
else if (body is UnaryExpression) | |
{ | |
var unaryExpression = body as UnaryExpression; | |
var nodeType = unaryExpression.NodeType; | |
if (unaryExpression.Operand is MethodCallExpression) | |
{ | |
var methodCallExpression = unaryExpression.Operand as MethodCallExpression; | |
var method = methodCallExpression.Method; | |
Console.WriteLine("Method name {0}", method.Name); | |
} | |
Console.WriteLine("Nodetype {0}", nodeType); | |
} | |
else if (body is BinaryExpression) | |
{ | |
var binaryExpression = body as BinaryExpression; | |
var nodeType = binaryExpression.NodeType; | |
Console.WriteLine("Nodetype {0}", nodeType); | |
} | |
} | |
} | |
public class Message | |
{ | |
public string Body { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment