Skip to content

Instantly share code, notes, and snippets.

@daanl
Created May 21, 2013 10:03
Show Gist options
  • Save daanl/5618733 to your computer and use it in GitHub Desktop.
Save daanl/5618733 to your computer and use it in GitHub Desktop.
Expressions
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