expression-trees

Expression tree - how to get at declaring instance?

假如想象 提交于 2019-12-07 11:24:01
问题 I'm a newbie when it comes to expression trees, so I'm not sure how to ask this question or what terminology to use. Here's an overly-simplifed version of what I'm trying to do: Bar bar = new Bar(); Zap(() => bar.Foo); public static void Zap<T>(Expression<Func<T>> source) { // HELP HERE: // I want to get the bar instance and call bar.Zim() or some other method. } How can I get to bar inside the Zap method? 回答1: Since the expression passed into your Zap method is a tree, you just need to walk

Retrieving an Expression from a property and adding it to an expression tree

回眸只為那壹抹淺笑 提交于 2019-12-07 11:23:58
问题 I've tried to simplify this example, as the actual code I'm playing with is more complex. So while this example may seem silly, bear with me. Let's say I'm working with the AdventureWorks database and I decide I want to add a property called Blarg to the Product table that returns an expression that contains code I would like to use in several places: public partial class Product { public Expression<Func<Product, string>> Blarg { get { return product => product.ProductModelID.HasValue ?

Performance of Mass-Evaluating Expressions in IronPython

这一生的挚爱 提交于 2019-12-07 11:14:28
问题 In an C#-4.0 application, I have a Dictionary of strongly typed ILists having the same length - a dynamically strongly typed column based table. I want the user to provide one or more (python-)expressions based on the available columns that will be aggregated over all rows. In a static context it would be: IDictionary<string, IList> table; // ... IList<int> a = table["a"] as IList<int>; IList<int> b = table["b"] as IList<int>; double sum = 0; for (int i = 0; i < n; i++) sum += (double)a[i] /

using expression trees to compare objects by a single property nets InvalidOperationException

被刻印的时光 ゝ 提交于 2019-12-07 09:35:32
I am trying to use Expression Trees because based on description, that seems to be the most correct (performant, configurable) approach. I expect to be able to craft a statement that gets the first item from the existingItems collection that matches the propertyNameToCompareOn value of the incomingItem. I have a method with the following signature and simulated code body... DetectDifferences<T>(List<T> incomingItems, List<T> existingItems) { var propertyNameToCompareOn = GetThisValueFromAConfigFile(T.FullName()); //does this belong outside of the loop? var leftParam = Expression.Parameter

Create delegate from constructor

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-07 06:58:04
问题 Using reflection, I'm trying to create a delegate from a parameterless constructor like this: Delegate del = GetMethodInfo( () => System.Activator.CreateInstance( type ) ).CreateDelegate( delType ); static MethodInfo GetMethodInfo( Expression<Func<object>> func ) { return ((MethodCallExpression)func.Body).Method; } But I get this exception: "Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type." What will work? Note

Building a custom predicate to act as a filter using a foreach loop

巧了我就是萌 提交于 2019-12-07 05:52:09
问题 I need to filter a list of documents by passing them to a custom filter that I'm struggling to build dynamically using a foreach loop : var mainPredicate = PredicateBuilder.True<Document>(); // mainPredicate is combined to other filters successfully here ... var innerPredicate = PredicateBuilder.False<Document>(); foreach (var period in periods) { var p = period; Expression<Func<Document, bool>> inPeriod = d => d.Date >= p.DateFrom && d.Date <= p.DateTo; innerPredicate = innerPredicate.Or(d =

How to compile an Expression down to the actual result?

你说的曾经没有我的故事 提交于 2019-12-07 05:40:54
问题 I am building an API around a web service call using Expressions to allow a developer to specify a query and have an ExpressionVisitor convert the Expression into the query string. The request is XML with a particular element containing a query string. For example, I can do something like this which will retrieve all checking accounts with a bank name of Bank 1 or Bank 2: "bankname = 'Bank 1' or bankname = 'Bank 2'" The web service can handle significantly more complex queries but I'll just

Expression.Convert doesn't throw InvalidOperationException for invariant value type parameters?

只愿长相守 提交于 2019-12-07 04:19:13
问题 Expression.Convert generally throws in InvalidOperationException when "No conversion operator is defined between expression.Type and type." The return type parameter of Func<> is covariant for reference types. // This works. Func<SomeType> a = () => new SomeType(); Func<object> b = a; It isn't covariant for value types. Variance applies only to reference types; if you specify a value type for a variant type parameter, that type parameter is invariant for the resulting constructed type. //

How to create an Expression Tree to do something similar to the SQL “Like ” command

◇◆丶佛笑我妖孽 提交于 2019-12-07 04:03:51
问题 I’m working on some expression tree code written by a colleague and am looking into the possibility of adding additional expressions. It currently supports: equals, not-equals, IsNull etc. I need to add something that will allow it to use a wildcard comparison similar to the SQL “Like” command or using regular expressions. At the moment the code parses an XML file and extracts the data which is then processed using code similar to the line shown below. This is an example of the “Equal”

Convert Expression<Func<T, T2, bool>> to Expression<Func<T2, bool>> by introducing a constant for T

孤人 提交于 2019-12-07 03:29:07
问题 I have an expression in the format of Expression<Func<T, T2, bool>> that I need to convert into an expression on the format of Expression<Func<T2, bool>> by replacing the T in the first expression with a constant value. I need this to stay as an expression so I can't just Invoke the expression with a constant as the first parameter. I've looked at the other questions here about expression trees but I can't really find a solution to my problem. I suspect I have to walk the expression tree to