I have seen this Issue while building dynamic Expression Tree and Expression/Statement trees and since I am new to expression trees I am still struggling to understand how to achieve what I want.
A contrived object is below
public class TestObject
{
public TestObject()
{
ClassList = new List<Class>();
}
public int Age { get; set; }
public List<Class> ClassList { get; set; }
}
public class Class
{
public string Name { get; set; }
public int ClassId { get; set; }
}
At run time I iterate through each of the properties and generate a Delegate which does a conversion to string of that property. I have got all that working. The issue I have to deal with now is that for the List type, I need to be able to apply a set of actions to each item in the ClassList property so I need a foreach which allows me to do that.
I currently have this
//type==TestObject at runtime
//propertyName == "ClassList"
ParameterExpression recordExpression = Expression.Parameter(type, "record");
memberExpression = MemberExpression.Property(recordExpression, propertyName);
Type getEnumerableDelegateType =
typeof(Func<,>).MakeGenericType(new Type[] { type, memberExpression.Type});
var getList = Expression.Lambda(getEnumerableDelegateType, memberExpression, recordExpression);
GetList when compiled and invoked returns the List as expected. What I m struggling with is how to create an expression which will use the result from the lambda expression and iterate over it applying the set of actions I have already created for each Class item.
Ultimately I am looking for a lambda signature to match the overallAction signature below
var getListFunc = new Func<TestObject, List<Class>>((TestObject obj1) => obj1.ClassList);
Action<List<Class>> listAction = delegate(List<Class> data)
{
foreach (var dataChannelWithUnitse in data)
{
//Apply generated delegate
}
};
Action<TestObject> overallAction = delegate(TestObject data)
{
var x = getListFunc.Invoke(data);
listAction.Invoke(x as List<Class>);
};
Any help is appreciated to help me understand how to do this.
I have currently got this which is exceptioning with variable 'Input' of type 'TestObject' referenced from scope '', but it is not defined
var typeParam = Expression.Parameter(type, "Input");
var listVariable = Expression.Variable(memberExpression.Type, "List");
var enumerator = Expression.Variable(typeof(IEnumerator<>).MakeGenericType(dataType));
var enumeratorType = typeof(IEnumerator<>).MakeGenericType(dataType);
var enumerableType = typeof(IEnumerable<>).MakeGenericType(dataType);
var enumerableParam = Expression.Parameter(enumerableType, "ExtractedCollection");
var getEnumeratorFunc = Expression.Call(enumerableParam, enumerableType.GetMethod("GetEnumerator"));
var getEnumeratorLambda = Expression.Lambda(getEnumeratorFunc, enumerableParam);
var t1 = Expression.Assign(listVariable, Expression.Invoke(getListLambda, typeParam));
var t2 = Expression.Assign(enumerator, Expression.Invoke(getEnumeratorLambda, listVariable));
var #break = Expression.Label();
var funcBlock = Expression.Block(
new ParameterExpression[] { listVariable, enumerator},
t1,
t2,
Expression.Loop(
Expression.IfThenElse(
Expression.NotEqual(Expression.Call(enumerator,typeof(IEnumerator).GetMethod("MoveNext")),Expression.Constant(false)),
Expression.Invoke(enumerableExpressions[0],Expression.Property(enumerator, "Current")),
Expression.Break(#break))
, #break), typeParam);
Expression<Action<TestObject>> lm = Expression.Lambda<Action<TestObject>>(funcBlock,recordExpression);
var d = lm.Compile(); **//this is exceptioning with " variable 'Input' of type 'TestObject' referenced from scope '', but it is not defined**
I got lost somewhere in the middle of your question (and if I've interpreted it incorrectly, please tell me, and I'll dive back into it), but I think this is what you're after:
public static Expression ForEach(Expression collection, ParameterExpression loopVar, Expression loopContent)
{
var elementType = loopVar.Type;
var enumerableType = typeof(IEnumerable<>).MakeGenericType(elementType);
var enumeratorType = typeof(IEnumerator<>).MakeGenericType(elementType);
var enumeratorVar = Expression.Variable(enumeratorType, "enumerator");
var getEnumeratorCall = Expression.Call(collection, enumerableType.GetMethod("GetEnumerator"));
var enumeratorAssign = Expression.Assign(enumeratorVar, getEnumeratorCall);
// The MoveNext method's actually on IEnumerator, not IEnumerator<T>
var moveNextCall = Expression.Call(enumeratorVar, typeof(IEnumerator).GetMethod("MoveNext"));
var breakLabel = Expression.Label("LoopBreak");
var loop = Expression.Block(new[] { enumeratorVar },
enumeratorAssign,
Expression.Loop(
Expression.IfThenElse(
Expression.Equal(moveNextCall, Expression.Constant(true)),
Expression.Block(new[] { loopVar },
Expression.Assign(loopVar, Expression.Property(enumeratorVar, "Current")),
loopContent
),
Expression.Break(breakLabel)
),
breakLabel)
);
return loop;
}
To use it, you need to supply a collection to iterate over, an expression to substitute into the body of the loop, and a ParameterExpression which is used by the loop body expression, which will be assigned to the loop variable on each loop iteration.
I think sometimes examples speak louder than words...
var collection = Expression.Parameter(typeof(List<string>), "collection");
var loopVar = Expression.Parameter(typeof(string), "loopVar");
var loopBody = Expression.Call(typeof(Console).GetMethod("WriteLine", new[] { typeof(string) }), loopVar);
var loop = ForEach(collection, loopVar, loopBody);
var compiled = Expression.Lambda<Action<List<string>>>(loop, collection).Compile();
compiled(new List<string>() { "a", "b", "c" });
EDIT: As Jeroem Mostert correctly points out in the comments, this doesn't quite mirror the "real" behaviour of a foreach loop: this would make sure that it disposes the enumerator. (It would also create a new instance of the loop variable for each iteration, but that doesn't make sense with expressions). Implementing this is just a matter of turning the handle if you feel motivated enough!
For anyone watching at home, I've got a similar method for generating 'for' loops:
public static Expression For(ParameterExpression loopVar, Expression initValue, Expression condition, Expression increment, Expression loopContent)
{
var initAssign = Expression.Assign(loopVar, initValue);
var breakLabel = Expression.Label("LoopBreak");
var loop = Expression.Block(new[] { loopVar },
initAssign,
Expression.Loop(
Expression.IfThenElse(
condition,
Expression.Block(
loopContent,
increment
),
Expression.Break(breakLabel)
),
breakLabel)
);
return loop;
}
This is equivalent to the following statement, where the pseudo-variables match the Expressions in the method above:
for (loopVar = initValue; condition; increment)
{
loopContent
}
Again, loopContent, condition, and increment are Expressions which uses loopVar, and loopVar is assigned on every iteration.
relatively_random's solution is great but foreach handles several other scenarios. Check these links to SharpLab to verify what is generated in each of them:
When the enumerable is a IEnumerable<T>, it checks if the enumerator is null before calling Dispose().
When the enumerable is not an interface, the enumerator changes to have the type returned by GetEnumerator(). The enumerator is cast to IDisposable before calling Dispose().
When the enumerator doesn't implement IDisposable, as is used on the check if the enumerator implements IDisposable. (???)
When the enumerator is a value type, the check for null goes away.
When the enumerator is a value type and doesn't implement IDisposable, the try/finally goes away.
The use of the type returned by GetEnumerator() is very important so that value type enumerators are not boxed. All the collections in System.Collections.Generic have value type enumerator because calls to its methods are not virtual, resulting in a lot better performance.
Putting all together results in the following code:
static partial class ExpressionEx
{
public static Expression ForEach<TSource>(Expression enumerable, Expression loopContent)
{
var enumerableType = enumerable.Type;
var getEnumerator = enumerableType.GetMethod("GetEnumerator");
if (getEnumerator is null)
getEnumerator = typeof(IEnumerable<>).MakeGenericType(typeof(TSource)).GetMethod("GetEnumerator");
var enumeratorType = getEnumerator.ReturnType;
var enumerator = Expression.Variable(enumeratorType, "enumerator");
return Expression.Block(new[] { enumerator },
Expression.Assign(enumerator, Expression.Call(enumerable, getEnumerator)),
EnumerationLoop(enumerator, loopContent));
}
public static Expression ForEach<TSource>(Expression enumerable, ParameterExpression loopVar, Expression loopContent)
{
var enumerableType = enumerable.Type;
var getEnumerator = enumerableType.GetMethod("GetEnumerator");
if (getEnumerator is null)
getEnumerator = typeof(IEnumerable<>).MakeGenericType(typeof(TSource)).GetMethod("GetEnumerator");
var enumeratorType = getEnumerator.ReturnType;
var enumerator = Expression.Variable(enumeratorType, "enumerator");
return Expression.Block(new[] { enumerator },
Expression.Assign(enumerator, Expression.Call(enumerable, getEnumerator)),
EnumerationLoop(enumerator,
Expression.Block(new[] { loopVar },
Expression.Assign(loopVar, Expression.Property(enumerator, "Current")),
loopContent)));
}
static Expression EnumerationLoop(ParameterExpression enumerator, Expression loopContent)
{
var loop = While(
Expression.Call(enumerator, typeof(IEnumerator).GetMethod("MoveNext")),
loopContent);
var enumeratorType = enumerator.Type;
if (typeof(IDisposable).IsAssignableFrom(enumeratorType))
return Using(enumerator, loop);
if (!enumeratorType.IsValueType)
{
var disposable = Expression.Variable(typeof(IDisposable), "disposable");
return Expression.TryFinally(
loop,
Expression.Block(new[] { disposable },
Expression.Assign(disposable, Expression.TypeAs(enumerator, typeof(IDisposable))),
Expression.IfThen(
Expression.NotEqual(disposable, Expression.Constant(null)),
Expression.Call(disposable, typeof(IDisposable).GetMethod("Dispose")))));
}
return loop;
}
public static Expression Using(ParameterExpression variable, Expression content)
{
var variableType = variable.Type;
if (!typeof(IDisposable).IsAssignableFrom(variableType))
throw new Exception($"'{variableType.FullName}': type used in a using statement must be implicitly convertible to 'System.IDisposable'");
var getMethod = typeof(IDisposable).GetMethod("Dispose");
if (variableType.IsValueType)
{
return Expression.TryFinally(
content,
Expression.Call(Expression.Convert(variable, typeof(IDisposable)), getMethod));
}
if (variableType.IsInterface)
{
return Expression.TryFinally(
content,
Expression.IfThen(
Expression.NotEqual(variable, Expression.Constant(null)),
Expression.Call(variable, getMethod)));
}
return Expression.TryFinally(
content,
Expression.IfThen(
Expression.NotEqual(variable, Expression.Constant(null)),
Expression.Call(Expression.Convert(variable, typeof(IDisposable)), getMethod)));
}
public static Expression While(Expression loopCondition, Expression loopContent)
{
var breakLabel = Expression.Label();
return Expression.Loop(
Expression.IfThenElse(
loopCondition,
loopContent,
Expression.Break(breakLabel)),
breakLabel);
}
}
The ForEach without loopVar is useful to enumerate without getting the items. That's the case of Count() implementation.
EDIT: An updated and tested version is available in the NetFabric.Reflection NuGet package. Check its repository for the source code.
Here's a slightly expanded version of canton7's excellent solution, taking into account the remarks about disposing the enumerator:
public static Expression ForEach(Expression enumerable, ParameterExpression loopVar, Expression loopContent)
{
var elementType = loopVar.Type;
var enumerableType = typeof(IEnumerable<>).MakeGenericType(elementType);
var enumeratorType = typeof(IEnumerator<>).MakeGenericType(elementType);
var enumeratorVar = Expression.Variable(enumeratorType, "enumerator");
var getEnumeratorCall = Expression.Call(enumerable, enumerableType.GetMethod("GetEnumerator"));
var enumeratorAssign = Expression.Assign(enumeratorVar, getEnumeratorCall);
var enumeratorDispose = Expression.Call(enumeratorVar, typeof(IDisposable).GetMethod("Dispose"));
// The MoveNext method's actually on IEnumerator, not IEnumerator<T>
var moveNextCall = Expression.Call(enumeratorVar, typeof(IEnumerator).GetMethod("MoveNext"));
var breakLabel = Expression.Label("LoopBreak");
var trueConstant = Expression.Constant(true);
var loop =
Expression.Loop(
Expression.IfThenElse(
Expression.Equal(moveNextCall, trueConstant),
Expression.Block(
new[] { loopVar },
Expression.Assign(loopVar, Expression.Property(enumeratorVar, "Current")),
loopContent),
Expression.Break(breakLabel)),
breakLabel);
var tryFinally =
Expression.TryFinally(
loop,
enumeratorDispose);
var body =
Expression.Block(
new[] { enumeratorVar },
enumeratorAssign,
tryFinally);
return body;
}
Related
I have the "problem" with the following expression tree that it creates a local variable inside the "Try" instead of setting the value directly for the LabelTarget. It's not necessarily a problem, yet I would like to understand why this happens and if I can somehow prevent the creation of the local variable as well.
Expression Tree:
var returnTarget = Expression.Label(typeof(string), "value");
var instanceArgument = Expression.Parameter(typeof(object), "instance");
var instanceAsType = Expression.Convert(instanceArgument, propertyInfo.DeclaringType);
var propertyValueAsType = Expression.Property(instanceAsType, propertyInfo.Name);
var exceptionParameter = Expression.Parameter(typeof(Exception), "exception");
Expression tryCatch = Expression.TryCatch(
Expression.Label(
returnTarget,
Expression.Call(
typeof(Enum).GetMethod("GetName", new[] { typeof(Type), typeof(object) }),
Expression.Constant(propertyInfo.PropertyType),
Expression.Convert(propertyValueAsType, typeof(object)))),
Expression.Catch(
exceptionParameter,
Expression.Label(
returnTarget,
Expression.Property(
exceptionParameter,
"Message"))));
return Expression.Lambda<Func<object, object>>(tryCatch, instanceArgument);
Resulting code:
public static object getValue_Enum(object instance)
{
string message;
try
{
// This should not happen. Instead, it should directly assign the value to "message"
string name = Enum.GetName((RuntimeType)typeof(Program.TestEnum), ((Program.Test2)instance).get_Enum());
message = name;
}
catch (Exception exception)
{
message = exception.Message;
}
return message;
}
My use case can have thousands of instances of a class. One of the properties in that class is also of the same type as that of a class and this can go on and on.
So I basically have a collection of nested classes. Now if I were to search a property's value, what would my options be.
I am finding recurssion as one(Getting Nested Object Property Value Using Reflection) and an exhaustive use of GetNestedTypes(https://msdn.microsoft.com/en-us/library/493t6h7t(v=vs.110).aspx) as other.
I read that reflection is expensive, so my question is, are there other ways to search the property without using reflection concept?
You can use expression trees to create an abstract syntax tree that you can then compile to a dynamic method. This performs very closely to regularly written code (from my testing it is many times faster then reflection). The creation of the dynamic method is expensive, so create once, use many times.
static Func<object,object> CreateDelegate(PropertyInfo[] path)
{
var rootType = path.First().DeclaringType;
var param = Expression.Parameter(typeof(object));
Expression access = Expression.Convert(param, rootType);
foreach (var prop in path)
{
access = Expression.MakeMemberAccess(access, prop);
}
var lambda = Expression.Lambda<Func<object, object>>(
Expression.Convert(access, typeof(object)),
param
).Compile();
return lambda;
}
static void Main(string[] args)
{
var path = new[]
{
typeof(Root).GetProperty("Level1"),
typeof(Level1).GetProperty("Level2"),
typeof(Level2).GetProperty("Name")
};
var method = CreateDelegate(path);
var data = new Root { Level1 = new Level1 { Level2 = new Level2 { Name = "Test" } } };
var result = method(data);
}
References: Dynamic Methods, Expression Tree-Compile
I am trying to construct function that accepts more that one parameter. Lambda function accepting one parameter works fine. Here is the code.
var value = 22.55;
var method = typeof(TextWriter).GetMethod("WriteLine", new Type[] { value.GetType() });
ParameterExpression inputParameter = Expression.Parameter(typeof(TextWriter));
var block = Expression.Block(
inputParameter,
Expression.Call(inputParameter, method, Expression.Constant(value)));
var function = Expression.Lambda<Action<TextWriter>>(block, inputParameter).Compile();
function(Console.Out);
However when I add one more parameter that I do not even use then function throws null reference exception. I must be missing something but have no idea what. Here is the code that does not work:
var value = 22.55;
var method = typeof(TextWriter).GetMethod("WriteLine", new Type[] { value.GetType() });
ParameterExpression inputParameter = Expression.Parameter(typeof(TextWriter));
// Additional parameter here
ParameterExpression inputParameter2 = Expression.Parameter(typeof(double));
var block = Expression.Block(
// Function block accepts two parameters
new List<ParameterExpression>() { inputParameter, inputParameter2 },
Expression.Call(inputParameter, method, Expression.Constant(value)));
var function = Expression.Lambda<Action<TextWriter, double>>(block, inputParameter, inputParameter2).Compile();
// ....aaand null exception here. Why?
function(Console.Out, value);
What am I doing wrong here?
Judging by your comment, you're misunderstanding what you're passing to Block. The variables argument of Block is to define local variables, rather than parameters.
var block = Expression.Block(
// Function block accepts two parameters
new List<ParameterExpression>() { inputParameter, inputParameter2 },
Expression.Call(inputParameter, method, Expression.Constant(value)));
Should be simply
var block = Expression.Block(Expression.Call(inputParameter, method, Expression.Constant(value)));
What you're currently doing is equivelant to:
void function(TextWriter tw, double d) {
TextWriter tw = default(TextWriter);
double d = default(double);
tw.WriteLine(22.55);
}
While you actually just want:
void function(TextWriter tw, double d) {
tw.WriteLine(22.55);
}
Also, you're not actually using your second parameter. The complete working code is:
var method = typeof(TextWriter).GetMethod("WriteLine", new Type[] { typeof(double) });
ParameterExpression inputParameter = Expression.Parameter(typeof(TextWriter));
ParameterExpression inputParameter2 = Expression.Parameter(typeof(double));
var block = Expression.Block(Expression.Call(inputParameter, method, inputParameter2));
var function = Expression.Lambda<Action<TextWriter, double>>(block, inputParameter, inputParameter2).Compile();
function(Console.Out, 35.5);
Your first code worked because you used a different overload of block. Instead of declaring a local variable for inputParameter, you passed it as an expression itself. So, the code looked like this:
void function(TextWriter tw, double d) {
tw; //Illegal for the C# compiler, but not illegal as an expression. This will do nothing.
tw.WriteLine(22.55);
}
I'm looking for a way to call a generic method with a lambda expression that calls Contains in an array of items.
In this case I'm using Entity Framework Where method, but the scenario could be applied in other IEnumerables.
I need to call the last line of the above code through Reflection, so I can use any type and any property to pass to the Contains method.
var context = new TestEntities();
var items = new[] {100, 200, 400, 777}; //IN list (will be tested through Contains)
var type = typeof(MyType);
context.Set(type).Where(e => items.Contains(e.Id)); //**What is equivalent to this line using Reflection?**
In research, I've noticed that I should use GetMethod, MakeGenericType and Expression to achieve that, but I couldn't figure out how to do it. It would be very helpful to have this sample so I can understand how Reflection works with Lambda and Generic concepts.
Basically the objective is to write a correct version of a function like this:
//Return all items from a IEnumerable(target) that has at least one matching Property(propertyName)
//with its value contained in a IEnumerable(possibleValues)
static IEnumerable GetFilteredList(IEnumerable target, string propertyName, IEnumerable searchValues)
{
return target.Where(t => searchValues.Contains(t.propertyName));
//Known the following:
//1) This function intentionally can't be compiled
//2) Where function can't be called directly from an untyped IEnumerable
//3) t is not actually recognized as a Type, so I can't access its property
//4) The property "propertyName" in t should be accessed via Linq.Expressions or Reflection
//5) Contains function can't be called directly from an untyped IEnumerable
}
//Testing environment
static void Main()
{
var listOfPerson = new List<Person> { new Person {Id = 3}, new Person {Id = 1}, new Person {Id = 5} };
var searchIds = new int[] { 1, 2, 3, 4 };
//Requirement: The function must not be generic like GetFilteredList<Person> or have the target parameter IEnumerable<Person>
//because the I need to pass different IEnumerable types, not known in compile-time
var searchResult = GetFilteredList(listOfPerson, "Id", searchIds);
foreach (var person in searchResult)
Console.Write(" Found {0}", ((Person) person).Id);
//Should output Found 3 Found 1
}
I'm not sure if the other questions address this scenario, because I don't think I could clearly understand how Expressions work.
Update:
I can't use Generics because I only have the type and the property to be tested (in Contains) at run-time. In the first code sample, suppose "MyType" is not known at compile time. In the second code sample, the type could be passed as a parameter to the GetFilteredList function or could be get via Reflection (GetGenericArguments).
Thanks,
After a wide research and a lot of study of Expressions I could write a solution myself. It certainly can be improved, but exactly fits my requirements. Hopefully it can help someone else.
//Return all items from a IEnumerable(target) that has at least one matching Property(propertyName)
//with its value contained in a IEnumerable(possibleValues)
static IEnumerable GetFilteredList(IEnumerable target, string propertyName, IEnumerable searchValues)
{
//Get target's T
var targetType = target.GetType().GetGenericArguments().FirstOrDefault();
if (targetType == null)
throw new ArgumentException("Should be IEnumerable<T>", "target");
//Get searchValues's T
var searchValuesType = searchValues.GetType().GetGenericArguments().FirstOrDefault();
if (searchValuesType == null)
throw new ArgumentException("Should be IEnumerable<T>", "searchValues");
//Create a p parameter with the type T of the items in the -> target IEnumerable<T>
var containsLambdaParameter = Expression.Parameter(targetType, "p");
//Create a property accessor using the property name -> p.#propertyName#
var property = Expression.Property(containsLambdaParameter, targetType, propertyName);
//Create a constant with the -> IEnumerable<T> searchValues
var searchValuesAsConstant = Expression.Constant(searchValues, searchValues.GetType());
//Create a method call -> searchValues.Contains(p.Id)
var containsBody = Expression.Call(typeof(Enumerable), "Contains", new[] { searchValuesType }, searchValuesAsConstant, property);
//Create a lambda expression with the parameter p -> p => searchValues.Contains(p.Id)
var containsLambda = Expression.Lambda(containsBody, containsLambdaParameter);
//Create a constant with the -> IEnumerable<T> target
var targetAsConstant = Expression.Constant(target, target.GetType());
//Where(p => searchValues.Contains(p.Id))
var whereBody = Expression.Call(typeof(Enumerable), "Where", new[] { targetType }, targetAsConstant, containsLambda);
//target.Where(p => searchValues.Contains(p.Id))
var whereLambda = Expression.Lambda<Func<IEnumerable>>(whereBody).Compile();
return whereLambda.Invoke();
}
In order to avoid using generics (since the types are not known at design time) you could use some reflection and build the expression "by hand"
You would need to do this by defining a "Contains" expression inside one Where clause:
public IQueryable GetItemsFromContainsClause(Type type, IEnumerable<string> items)
{
IUnitOfWork session = new SandstoneDbContext();
var method = this.GetType().GetMethod("ContainsExpression");
method = method.MakeGenericMethod(new[] { type });
var lambda = method.Invoke(null, new object[] { "Codigo", items });
var dbset = (session as DbContext).Set(type);
var originalExpression = dbset.AsQueryable().Expression;
var parameter = Expression.Parameter(type, "");
var callWhere = Expression.Call(typeof(Queryable), "Where", new[] { type }, originalExpression, (Expression)lambda);
return dbset.AsQueryable().Provider.CreateQuery(callWhere);
}
public static Expression<Func<T, bool>> ContainsExpression<T>(string propertyName, IEnumerable<string> values)
{
var parameterExp = Expression.Parameter(typeof(T), "");
var propertyExp = Expression.Property(parameterExp, propertyName);
var someValue = Expression.Constant(values, typeof(IEnumerable<string>));
var containsMethodExp = Expression.Call(typeof(Enumerable), "Contains", new[] { typeof(string) }, someValue, propertyExp);
return Expression.Lambda<Func<T, bool>>(containsMethodExp, parameterExp);
}
In this case "Codigo" is hard-coded, but it could be a parameter to get any property of the type you define.
You could test it by using:
public void LambdaConversionBasicWithEmissor()
{
var cust= new Customer();
var items = new List<string>() { "PETR", "VALE" };
var type = cust.GetType();
// Here you have your results from the database
var result = GetItemsFromContainsClause(type, items);
}
You can solve your problem by using the following set of classes.
First, we need to create a Contains class which will decide which items will be chosen from the source array.
class Contains
{
public bool Value { get; set; }
public Contains(object[] items, object item)
{
Value = (bool)(typeof(Enumerable).GetMethods()
.Where(x => x.Name.Contains("Contains"))
.First()
.MakeGenericMethod(typeof(object))
.Invoke(items, new object[] { items, item }));
}
}
Then we need to create a Where class which will be used to form a predicate based on which items will be selected. It should be clear that in our case, we are going to use the Contains class for our predicate method.
class Where
{
public object Value { get; set; }
public Where(object[] items, object[] items2)
{
Value = typeof(Enumerable).GetMethods()
.Where(x => x.Name.Contains("Where"))
.First()
.MakeGenericMethod(typeof(object))
.Invoke(items2, new object[] { items2, new Func<object, bool>(i => new Contains(items, i).Value) });
}
}
The last step is simply to invoke the result we got from the Where class, which is actually of type Enumerable.WhereArrayIterator and not of type List, since the result of the Where Extension method is a product of deferred execution.
Thus we need to create a non deferred object, by calling its ToList Extension Method, and get our result.
class ToList
{
public List<object> Value { get; set; }
public ToList(object[] items, object[] items2)
{
var where = new Where(items, items2).Value;
Value = (typeof(Enumerable).GetMethods()
.Where(x => x.Name.Contains("ToList"))
.First()
.MakeGenericMethod(typeof(object))
.Invoke(where, new object[] { where })) as List<object>;
}
}
In the end, you can simply test the whole process out by using the following class.
class Program
{
static void Main()
{
var items = new object[] { 1, 2, 3, 4 };
var items2 = new object[] { 2, 3, 4, 5 };
new ToList(items, items2).Value.ForEach(x => Console.WriteLine(x));
Console.Read();
}
}
I'm using a combination of reflection and expression trees, and want to pass back certain property accessors from a class to a calling method. My current code has a method traversing the class and returning a list of MemberExpressions. The caller then iterates over the member expressions and creates lambdas, which should then be called with an instance of the inspected class to return the value of the property.
Here is a sample of what it would look like without the method calls (Runnable in LINQPad):
void Main()
{
var t = new Test { Prop = "Test" };
var property = t.GetType().GetProperty("Prop");
var baseType = Expression.Parameter(typeof(Test), "baseType");
var memberAccess = Expression.MakeMemberAccess(baseType, property);
var lambda = Expression.Lambda<Func<Test, string>>(memberAccess, Expression.Parameter(typeof(Test), "baseType"));
var func = lambda.Compile();
var result = func(t);
result.Dump();
}
class Test {
public string Prop { get; set; }
}
This does not work, throwing this exception:
InvalidOperationException: variable 'baseType' of type 'UserQuery+Test' referenced from scope '', but it is not defined
However, if I change the creation of the lambda to this:
var lambda = Expression.Lambda<Func<Test, string>>(memberAccess, baseType);
That is, replace the Expression.Parameter with the variable used earlier, then it works. This is not (easily) possible in the scenario where I want to use it, since I would have to return the original parameter along with the list (I could return a tuple, of course, but I would prefer not to, if it is not necessary).
Why does it work like this? Inspecting the DebugView of the lambda, they are exactly the same no matter what approach is used:
.Lambda #Lambda1<System.Func`2[UserQuery+Test,System.String]>(UserQuery+Test $baseType)
{
$baseType.S
}
Yes, you need to refer ParameterExpression, used earlier. This won't compile too:
private String Foo(Test myParam)
{
return myAnotherParam.MyProperty;
}
With instatiating new ParameterExpression in lambda, you're doing the same thing (but note, when making lambda, you're doing it in reversed order - first, you're constructing a method body, then - a method declaration):
// return myAnotherParam.MyProperty;
var baseType = Expression.Parameter(typeof(Test), "baseType");
var memberAccess = Expression.MakeMemberAccess(baseType, property);
// private String Foo(MyClass myParam)
var lambda = Expression.Lambda<Func<Test, string>>(memberAccess, Expression.Parameter(typeof(Test), "baseType"));