Test classes
public class Foo
{
public Bar Bar { get; set; }
}
public class Bar
{
public string Baz { get; set; }
}
public class BindFoo
{
public string BarBaz { get; set; }
}
Snippet
Expression<Func<Foo, object>> baz = x => x.Bar.Baz;
var param = Expression.Parameter(typeof(Foo), "x");
var bindings = typeof(BindFoo)
.GetProperties()
.Select(x => Expression.Bind(x, (MemberExpression)baz.Body))
.OfType<MemberBinding>()
.ToArray();
var expression = Expression.Lambda<Func<Foo, object>>(
Expression.MemberInit(
Expression.New(typeof(BindFoo).GetConstructor(Type.EmptyTypes)),
bindings),
param);
var func = expression.Compile();
Throws the 'x' not defined error at expression.Compile() when the property is nested. How do I bind the nested property Bar.Baz?
The expression built in the code above is x => new BindFoo() {BarBaz = x.Bar.Baz}, which is what I want but I think the x.Bar.Baz had not been bound correctly.
The problem is not related to Bind and nested members, but the parameter of the dynamically created lambda expression.
Parameters bind by instance, not by name. Here
Expression<Func<Foo, object>> baz = x => x.Bar.Baz;
var param = Expression.Parameter(typeof(Foo), "x");
you defined a new parameter, but then trying to use baz.Body which is bound to it's own parameter.
The solution is to use the original parameter
Expression<Func<Foo, object>> baz = x => x.Bar.Baz;
var param = baz.Parameters[0];
or replace the baz.Parameters[0] with the new parameter using expression visitor.
Related
I want to create an expression for converting a property from an Enum to an Int.
I want to use DynamicExpressionParser.ParseLambda to parse nested properties:
public enum MyEnum { A, B };
public class OuterClass
{
public InnerClass Inner { get; set; }
public MyEnum Enum { get; set; }
}
public class InnerClass
{
public MyEnum Enum { get; set; }
}
DynamicExpressionParser.ParseLambda<OuterClass, int>(null, true, "Inner.Enum");
But this will throw an exception:
Expression of type 'Int32' expected (at index 0)
How can I convert the Enum to an integer inside the expression?
It works with the following method, but i cannot use nested properties there, so it will only work when I place the "Enum" property on the OuterClass:
public static Expression<Func<T, TProperty>> GetExpression<T, TProperty>(string propertyName)
{
// x =>
var parameter = Expression.Parameter(typeof(T));
// x.Name
var mapProperty = Expression.Property(parameter, propertyName);
// (object)x.Name
var convertedExpression = Expression.Convert(mapProperty, typeof(TProperty));
// x => (object)x.Name
return Expression.Lambda<Func<T, TProperty>>(convertedExpression, parameter);
}
GetExpression<OuterClass, int>("Enum");
I found a solution:
// Create expression for it-type OuterClass. Pass null for result type which will result in an expression with the result type of the actual enum type.
var enumExpression = DynamicExpressionParser.ParseLambda(typeof(OuterClass), null, "Inner.Enum");
// Convert the result type of the body to int which will result in a converted expression
var convertedBodyExpression = Expression.Convert(enumExpression.Body, typeof(int));
// Create the final expression
var expression = Expression.Lambda<Func<OuterClass, int>>(convertedBodyExpression, enumExpression.Parameters);
In order to use it as a selector in a Grouping Clause at runtime, I want basically retrieve a property from an object T by passing its name. Example with this class:
class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime Birthday { get; set; }
}
I want to use my method like this:
private void MyMethod(string propertyName)
{
var lambda = GetLambdaExpression(propertyName);
// The problem is to specify the Type at compile time...
var selector = GetLambdaSelector<DateTime>(lambda); // Seems mandatory to convert LambdaExpression to Expression<Func<Person, T>> to use in a grouping clause
var result = emps.GroupBy(
selector.Compile(),
p => p.Birthday,
(key, g) => new { PersonName = key, = g.ToList() }).AsQueryable();
}
Here are methods statements :
LambdaExpression GetLambdaExpression(string propertyName)
{
var type = typeof(Person);
var propertyInfo = type.GetProperty(propertyName);
var propertyType = propertyInfo.PropertyType;
var entityType = propertyInfo.DeclaringType;
var parameter = Expression.Parameter(entityType, "entity");
var property = Expression.Property(parameter, propertyInfo);
var funcType = typeof(Func<,>).MakeGenericType(entityType, propertyInfo.PropertyType);
var lambda = Expression.Lambda(funcType, property, parameter);
return lambda;
}
Expression<Func<Person, T>> GetLambdaSelector<T>(LambdaExpression expression)
{
return (Expression<Func<Person, T>>)expression;
}
I'm stuck because I want to resolve the type (DateTimein the example) at runtime and not at compile time.
So how to call this method with type resolve at runtime:
GetLambdaSelector<?>(lambda); ?
Or maybe can I change the signature of LambdaExpression GetLambdaExpression(string propertyName) to Expression<Func<Person, T>> with reflection but I did not manage it for the moment. ?
I have a class and I need to iterate tru each property reading the attribute name to map to my data Source the value, in the cases where I have a ICollection that property will have multiple attributes to map the correct value.
I'm using Expression trees to set the values efficiently to each property but I'm having issues to set the values to the Collection.
I think this is because I need to create an instance of that Collection but I don't know. I'm a bit lost on that one here's what I got:
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class MapToAttribute : Attribute
{
public MapToAttribute(string field)
{
Field = field;
}
public string Field { get; private set; }
}
public class MyDataClass
{
[MapTo("one")]
public int propOne { get; set; }
[MapTo("two")]
public string propTwo { get; set; }
[MapTo("item1")]
[MapTo("item2")]
[MapTo("item3")]
public ICollection<int> collection { get; set; }
}
class Program
{
static void Main(string[] args)
{
var setter = SetValues<MyDataClass>();
}
private static IEnumerable<T> SetValues<T>()
where T : new()
{
var properties = GetClassProperties<T>();
var results = new List<T>();
for (int x = 1; x<=100; x++)
{
var row = new T();
//Simulated Datasource
var dataSource = new Dictionary<string, object>();
dataSource.Add("one", x);
dataSource.Add("two", x.ToString());
dataSource.Add("item1", x);
dataSource.Add("item2", x+x);
dataSource.Add("item3", x*x);
foreach (var property in properties)
{
//this line executes the Action
property.Value(row, dataSource[property.Key]);
}
results.Add(row);
}
return results;
}
private static Dictionary<string, Action<T, object>> GetClassProperties<T>()
{
var setters = new Dictionary<string, Action<T, object>>();
var instance = Expression.Parameter(typeof(T));
var argument = Expression.Parameter(typeof(object));
foreach (var property in typeof(T).GetProperties())
{
var names = property.GetCustomAttributes(typeof(MapToAttribute), true)
.Select(p => ((MapToAttribute)p).Field);
var setter = Expression.Lambda<Action<T, object>>(
Expression.Call(
instance,
property.GetSetMethod(),
Expression.Convert(argument, property.PropertyType)
), instance, argument
).Compile();
// Due to the types I cannot just assign a value to a ICollection,
// that's why I tried to create HERE a different setter
// when the property Type is ICollection, I commented out the code failing.
//var getCollection = Expression.Lambda<Func<T, object>>(
// Expression.Call(
// instance,
// prop.GetGetMethod()
// ), instance
// ).Compile();
//Action<T, object> setter = (classInstance, value) =>
// getCollection(classInstance).Add(value);
foreach (var name in names)
{
setters.Add(name, setter);
}
}
return setters;
}
}
First of all to make life easier you will need to initialize collection:
public ICollection<int> collection { get; set; } = new List<int>();
Second try this:
private static Dictionary<string, Action<T, object>> GetClassProperties<T>()
{
var setters = new Dictionary<string, Action<T, object>>();
var instance = Expression.Parameter(typeof(T));
var argument = Expression.Parameter(typeof(object));
foreach (var property in typeof(T).GetProperties())
{
var names = property.GetCustomAttributes(typeof(MapToAttribute), true)
.Select(p => ((MapToAttribute)p).Field)
.ToList();
if (property.PropertyType.IsGenericType) // start checking that prop implements ICollection
{
// get ICollection generic parameter type
var genericParam = property.PropertyType.GetGenericArguments().First();
// construct concrete ICollection type
var propColType = typeof(ICollection<>).MakeGenericType(genericParam);
if (propColType.IsAssignableFrom(property.PropertyType)) // check if is ICollection of genericParam
{
var getCollection = Expression.Call(instance, property.GetGetMethod());
var addMethod = propColType.GetMethod("Add");
var colAddSetter = Expression.Lambda<Action<T, object>>(
Expression.Call(getCollection, addMethod, Expression.Convert(argument, genericParam)),
instance, argument)
.Compile();
foreach (var name in names)
{
setters.Add(name, colAddSetter);
}
continue; // process next property
}
}
// process "ordinary" property
var setter = Expression.Lambda<Action<T, object>>(
Expression.Call(
instance,
property.GetSetMethod(),
Expression.Convert(argument, property.PropertyType)
), instance, argument
).Compile();
setters.Add(names.Single(), setter); // todo throw normal exception instead of Single
}
return setters;
}
I have a class with a bunch of properties:
class Foo {
public string Name {get; set; }
public int Age {get; set;
}
and a collection of instances of Foo.
Now I want to order those elements by a property given by the user. So the user selects a property from the type Foo. Now I want to order by elements based on this property.
One approach is a reflection-based one similar to this:
var p = typeof(Foo).GetProperty("Age");
var ordered = fooList.OrderBy(x => (int) p.GetValue(x, null));
This works so far. However I also tried a second one and there I am stuck. It deals by performing an expression-tree as follows:
var f = GetOrderStatement<Foo>("Age");
var ordered = fooList.OrderBy(f)
With
Func<T, int> GetOrderStatement<T>(string attrName)
{
var type = Expression.Parameter(typeof(T), attrName);
var property = Expression.PropertyOrField(type, attrName);
return Expression.Lambda<Func<T, int>>(property).Compile();
}
My question is: As I should return a Func<T, int> where to get the int-part from or in other words where and how do I perform the actual comparison? I suppose I have to make a CallExpression to IComparable.CompareTo but I´m not sure how to do so. I think I need access to the both instances to compare.
EDIT: Complete code-example
static void Main()
{
var fooList = new[] { new Foo("Hans", 10), new Foo("Georg", 12), new Foo("Birgit", 40) };
var f = GetOrderStatement<Foo>("Age");
var ordered = fooList.OrderBy(f);
}
private static Func<T, int> GetOrderStatement<T>(string attrName)
{
var type = Expression.Parameter(typeof(T), attrName);
var property = Expression.PropertyOrField(type, attrName);
return Expression.Lambda<Func<T, int>>(property).Compile();
}
Executing this code will throw an
ArgumentException: Incorrect number of parameters supplied for lambda
declaration
The problem is that you're trying to build a Func<T, int> but your call to Expression.Lambda doesn't specify the parameter expression, which means you can't expect it to create a delegate that has any parameters. Just specifying type as a second argument to Expression.Lambda works. Here's a complete example based on your question - note that I've changed the ages to prove that it's actually ordering, and I've updated your fields to read-only properties:
using System;
using System.Linq;
using System.Linq.Expressions;
class Foo
{
public string Name { get; }
public int Age { get; }
public Foo(string name, int age)
{
this.Name = name;
this.Age = age;
}
}
class Test
{
static void Main()
{
var fooList = new[]
{
new Foo("Hans", 12),
new Foo("Georg", 10),
new Foo("Birgit", 40)
};
var f = GetOrderStatement<Foo>("Age");
var ordered = fooList.OrderBy(f);
foreach (var item in ordered)
{
Console.WriteLine($"{item.Name}: {item.Age}");
}
}
private static Func<T, int> GetOrderStatement<T>(string attrName)
{
var type = Expression.Parameter(typeof(T), attrName);
var property = Expression.PropertyOrField(type, attrName);
return Expression.Lambda<Func<T, int>>(property, type).Compile();
}
}
Output:
Georg: 10
Hans: 12
Birgit: 40
I want to create dynamic lambda expressions so that I can filter a list using a set of filtering parameters. This is what I have so far:
The expression is built using the methods bellow, where T is the object type of the list
public static Expression<Func<T, bool>> GetExpression<T>(IList<DynamicFilter> filters)
{
if (filters.Count == 0)
return null;
ParameterExpression param = Expression.Parameter(typeof(T), "t");
Expression exp = null;
if (filters.Count == 1)
exp = GetExpression<T>(param, filters[0]);
[...]
return Expression.Lambda<Func<T, bool>>(exp, param);
}
private static Expression GetExpression<T>(ParameterExpression param, DynamicFilter filter)
{
MemberExpression member = Expression.Property(param, filter.PropertyName);
ConstantExpression constant = Expression.Constant(filter.Value);
[...]
return Expression.Call(member, filterMethod, constant);
}
I then call
List<Example> list = ...;
var deleg = ExpressionBuilder.GetExpression<Example>(dynFiltersList).Compile();
list = list.Where(deleg).ToList();
This works just as expected with an object that contains only simple types, but if there are complex types inside, the code doesn't work anymore. For example, let's say I have a member of custom type Field inside the Example class and Field has a string property Value. If filter.PropertyName would be 'Field0' (of type Field), the code would work just fine, but if I have 'Field0.Value' I would get an obvious error stating that there is no property named 'Field0.Value' inside class Example.
I tried modifying the expression building method, like this:
MemberExpression member = null;
if (filter.PropertyName.Contains('.'))
{
string[] props = filter.PropertyName.Split('.');
ParameterExpression param1 = Expression.Parameter(typeof(T).GetProperty(props[0]).PropertyType, "t1");
member = Expression.Property(param1, props[0]);
}
else
{
member = Expression.Property(param, filter.PropertyName);
}
but then I got a Lambda parameter not in scope error when compiling the expression. I sort of understand why I get this error, but I don't know how to make this work.
Bottom line is I need to make the expression building method work recursively when forming the MemberExpression. I ultimately need to obtain a list = list.Where(deleg).ToList(); that translates to something like this list = list.Where(obj => obj.Field0.Value == 'something').ToList();
I've just started working with expressions so I don't really know too much in this area, but any help would be appreciated.
Thanks
I realize this is a fairly old post, but I had the exact same problem and found something close in an answer that Mark Gravell posted here. I just modified it slightly to meet my needs and below is the result:
private Expression GetDeepProperty(Expression parameter, string property)
{
var props = property.Split('.');
var type = parameter.Type;
var expr = parameter;
foreach (var prop in props)
{
var pi = type.GetProperty(prop);
expr = Expression.Property(expr, pi);
type = pi.PropertyType;
}
return expr;
}
Implementation:
var method = typeof (string).GetMethod("Contains", new Type[] {typeof (string)}, null);
var lambdaParameter = Expression.Parameter(typeof(TEntity), "te");
var filterExpression = Expression.Lambda<Func<TEntity, bool>> (
filters.Select(filter => Expression.Call(GetDeepProperty(lambdaParameter, filter.Property),
method,
Expression.Constant(filter.Value))).
Where(exp => exp != null).
Cast<Expression>().
ToList().
Aggregate(Expression.Or), lambdaParameter);
I'm trying to address
so that I can filter a list using a set of filtering parameters
not by using ExpressionBuilder but instead by using a generic Filter class.
public class Filter<T> where T: class
{
private readonly Predicate<T> criteria;
public Filter(Predicate<T> criteria)
{
this.criteria = criteria;
}
public bool IsSatisfied(T obj)
{
return criteria(obj);
}
}
First we need to have some classes.
public class Player
{
public string Name { get; set; }
public int Level { get; set; }
public enum Sex { Male, Female, Other };
public Weapon Weapon { get; set; }
}
public class Weapon
{
public string Name { get; set; }
public int MaxDamage { get; set; }
public int Range { get; set; }
public WeaponClass Class { get; set; }
public enum WeaponClass { Sword, Club, Bow }
}
Then we need a list of objects.
var graywand = new Weapon { Name = "Graywand", MaxDamage = 42, Range = 1, Class = Weapon.WeaponClass.Sword };
var scalpel = new Weapon { Name = "Scalpel", MaxDamage = 33, Range = 1, Class = Weapon.WeaponClass.Sword };
var players = new List<Player> {
new Player { Name = "Fafhrd", Level = 19, Weapon = graywand },
new Player { Name = "Gray Mouser", Level = 19, Weapon = scalpel },
new Player { Name = "Freddy", Level = 9, Weapon = graywand },
new Player { Name = "Mouse", Level = 8, Weapon = scalpel}
};
Then let's create a couple of filters and add those to a list.
var powerfulSwords = new Filter<Player>(p => p.Weapon.MaxDamage>35);
var highLevels = new Filter<Player>(p => p.Level>15);
var filters = new List<Filter<Player>>();
filters.Add(powerfulSwords);
filters.Add(highLevels);
Finally filter the list by those filters
var highLevelAndPowerfulSwords = players.Where(p => filters.All(filter => filter.IsSatisfied(p)));
var highLevelOrPowerfulSwords = players.Where(p => filters.Any(filter => filter.IsSatisfied(p)));
Only "Fafhrd" will be in highLevelAndPowerfulSwords and highLevelOrPowerfulSwords will contain all players but "Mouse".
Have a look at ExpressionVisitor as described here: Replacing the parameter name in the Body of an Expression
You could generate an expression for each element in filter and combine them into one single expression using the methods below:
public static Expression<Func<T, K>> CombineAnd<T, K>(Expression<Func<T, K>> a, Expression<Func<T, K>> b)
{
ParameterExpression firstParameter = a.Parameters.First();
Expression<Func<T, K>> b1 = Expression.Lambda<Func<T, K>>(Expression.Invoke(b, firstParameter), firstParameter);
return Expression.Lambda<Func<T, K>>(Expression.And(a.Body, b1.Body), firstParameter);
}
public static Expression<Func<T, K>> CombineOr<T, K>(Expression<Func<T, K>> a, Expression<Func<T, K>> b)
{
ParameterExpression firstParameter = a.Parameters.First();
Expression<Func<T, K>> b1 = Expression.Lambda<Func<T, K>>(Expression.Invoke(b, firstParameter), firstParameter);
return Expression.Lambda<Func<T, K>>(Expression.Or(a.Body, b1.Body), firstParameter);
}