expression-trees

Convert an Expression<Func<T,bool>> to an Expression<Func<T1,bool>> so that T is a member of T1

假如想象 提交于 2019-12-23 10:55:13
问题 We have an entity of type T1 which has a member of type T . something like this : public class T1 { public T Member{get;set;} } User can use our UI to give us a filter over T and we have translate it to an expression of a function that gets a T and returns bool (Expression<Func<T,bool>>) I would like to know is it possible to convert this to an expression of a function that gets T1 and returns bool. Actually I'd like to convert this : (t=>t.Member1==someValue && t.Member2==someOtherValue); to

Body from Func<T>

懵懂的女人 提交于 2019-12-23 09:34:51
问题 how can I get a body from function Func<bool> methodCall = () => output.SendToFile(); if (methodCall()) Console.WriteLine("Success!"); I need to get this output.SendToFile() as a string Another example: string log = ""; public void Foo<T>(Func<T> func) { try { var t = func(); } catch (Exception) { //here I need to add the body of the lambda // log += func.body; } } public void Test() { var a = 5; var b = 6; Foo(() => a > b); } Edit: For more information on this topic see: Expression Trees 回答1

Is there a way to set 'DeclaringType' in an expression tree?

我与影子孤独终老i 提交于 2019-12-23 09:19:30
问题 I am doing a Func -> Expression -> Func conversion. It works fine if I create the Func<>() from a method(first example below) however if I create the function using an expression tree(2nd example) it fails with a NullReferenceException when accessing func2.Method.DeclaringType.FullName . And this is because DeclaringType is null. (NJection uses reflection so I think that is why it needs DeclaringType.) How can I fill in DeclaringType type for the Func<> that was created by compiling an

expression trees linq get value of a parameter?

我们两清 提交于 2019-12-23 05:46:14
问题 AddOptional<tblObject>(x =>x.Title, objectToSend.SupplementaryData); private static void AddOptional<TType>(Expression<Func<TType,string>> expr, Dictionary<string, string> dictionary) { string propertyName; string propertyValue; Expression expression = (Expression)expr; while (expression.NodeType == ExpressionType.Lambda) { expression = ((LambdaExpression)expression).Body; } } In Above code i would like to get actual value of property title, not ony propert name , is it possible ? 回答1:

How can I calculate an expression parameter in run time?

ぐ巨炮叔叔 提交于 2019-12-23 05:15:12
问题 The problem I need to solve is to run data through IF statements. The IF statements are generated by a SQL table on run time. I've managed to do this by using expressions and Lambda expressions. My table has memberName; Operator; Target. So for example I get "Age", "GreaterThan" and "40" from the table, and I compile it. var rule = new Rule(rowRule["memberName"].ToString(), rowRule["Operator"].ToString(), rowRule["Target"].ToString()); Func<User, bool> compiledRule = CompileRule<User>(rule);

How to use expression trees in PHP?

喜你入骨 提交于 2019-12-23 04:09:27
问题 I'm currently making a PHP-program that solves equations. I've divided the input equation into an array so that each line in the array contains one term. (So. [0] = "+5x", [1] = "=", [2] = "-5", [3] = "+10". This is just a basic equation. The script also divides what is in () into sub-arrays. So for example 2x+(5-3x)=3(x+1) [0] = "+2*x", [1] = array([0] = "+5".... However, I discovered expression trees that is the perfect thing to use in this equation-solver. I've searched the whole internet

How do I generate a compiled lambda with method calls?

你说的曾经没有我的故事 提交于 2019-12-23 03:16:07
问题 I'm generating compiled getter methods at runtime for a given member. Right now, my code just assumes that the result of the getter method is a string (worked good for testing). However, I'd like to make this work with a custom converter class I've written, see below, "ConverterBase" reference that I've added. I can't figure out how to add the call to the converter class to my expression tree. public Func<U, string> GetGetter<U>(MemberInfo info) { Type t = null; if (info is PropertyInfo) { t

Reverse of Expression<Func<T,TResult>>.Compile()?

坚强是说给别人听的谎言 提交于 2019-12-22 09:56:20
问题 Since we can: Expression<Func<int, bool>> predicate = x => x > 5; var result = Enumerable.Range(0,10).Where(predicate.Compile()); How can I: Func<int,bool> predicate = x => x > 5; Expression<Func<int,bool>> exp = predicate.Decompile(); That is, I want to get the corresponding Expression of the Func . Is it possible? 回答1: There is no magic Decompile() for a delegate instance, short of deconstructing the IL (perhaps with mono.cecil). If you want an expression tree, you'll have to start with an

Expression Tree with Property Inheritance causes an argument exception

戏子无情 提交于 2019-12-22 08:25:32
问题 Following this post: link text I'm trying to create an expression tree that references the property of a property. My code looks like this: public interface IFoo { void X {get;set;} } public interface IBar : IFoo { void Y {get;set;} } public interface IFooBarContainer { IBar Bar {get;set;} } public class Filterer { //Where T = "IFooBarContainer" public IQueryable<T> Filter<T>(IEnumerable<T> collection) { var argument = Expression.Parameter(typeof (T), "item"); //... //where propertyName =

Compile expression that requires a parameter

橙三吉。 提交于 2019-12-22 08:18:57
问题 Ok, I am sure this is simple, but I am having a senior moment. I have a simple BinaryExpression (greaterthan) the left side is a ParameterExpression and the right side is a ConstantExpression I want to compile this expression to a func that I can call and pass a parameter to... var func = ...something with my exp.... bool result = func(myValue); Thanks to Hasan, I modified his answer to my needs... var func = Expression.Lambda<Func<int,bool>>(myExpr, (ParameterExpression)myExpr.left).Compile(