expression-trees

How to get Property Value from MemberExpression without .Compile()?

*爱你&永不变心* 提交于 2019-12-03 14:46:53
I'm having issues trying to get the value of an object out of the Expression Tree without using .Compile() The object is quite simple. var userModel = new UserModel { Email = "John@Doe.com"}; The method giving me issues looks like this. private void VisitMemberAccess(MemberExpression expression, MemberExpression left) { var key = left != null ? left.Member.Name : expression.Member.Name; if (expression.Expression.NodeType.ToString() == "Parameter") { // add the string key _strings.Add(string.Format("[{0}]", key)); } else { // add the string parameter _strings.Add(string.Format("@{0}", key)); //

How to create an empty delegate using Expression Trees?

元气小坏坏 提交于 2019-12-03 14:26:14
Using anonymous methods you can create empty delegates since C# 2.0. public event EventHandler SomeEvent = delegate {}; public event Action OtherEvent = delegate {}; This is e.g. useful to prevent having to do the null check when invoking events . How can I create the same behavior using Expression Trees ? The only possible option I see now is to use Expression.Lambda() , but as far as I can tell this would require a lot of extra work. An expression tree, by nature of its purpose, always has an expression for a body rather than a statement in the original design. In C# 3 there was no way at

dynamic sort in linq

一个人想着一个人 提交于 2019-12-03 13:21:17
问题 please consider this scenario: I have a list of a class with about 50 fields.I want to have a Combobox that user can select according to what field list will sort.For example if user select "F1" list sort according to "F1". I don't want to sort with if-else for every fields.I see this topic : Sorting a gridview when databinding a collection or list of objects but I can't use of it's answer. How I can use Expression Tree for this purpose? thanks Edit 1) : According to dear @Thom Smith answer I

Performing part of a IQueryable query and deferring the rest to Linq for Objects

五迷三道 提交于 2019-12-03 13:10:46
I have a Linq provider that sucessfully goes and gets data from my chosen datasource, but what I would like to do now that I have my filtered resultset, is allow Linq to Objects to process the rest of the Expression tree (for things like Joins, projection etc) My thought was that I could just replace the expression constant that contains my IQueryProvider with the result-sets IEnumerable via an ExpressionVisitor and then return that new expression. Also return the IEnumerable's provider from my IQueryable...but this does not seem to work :-( Any idea's? Edit: Some good answers here, but given

How do I infer the usage of parentheses when translating an expression tree?

泄露秘密 提交于 2019-12-03 12:53:52
I am working on translating an expression tree to a format that resembles infix notation; I am not evaluating the tree or executing its operations. The tree contains both logical and relational operations, and I would like to emit parentheses in an intelligent manner during the translation. To illustrate, consider the following contrived expression: a < x & (a < y | a == c) & a != d If I walk the expression tree produced by this expression in-order, then I will print out the following expression, which is incorrect. a < x & a < y | a == c & a != d // equivalent to (a < x & a < y) | (a == c & a

Compiling lambdas and invoking delegates on the device in Monotouch

青春壹個敷衍的年華 提交于 2019-12-03 12:40:37
I am currently porting a .NET codebase in MonoTouch and I'm currently working on a method that receives an Expression<T> . I'm trying to compile it, and then dynamically invoke it. Here's what I did: // Here's an example of what I could receive Expression<Action<int>> expression = (a => Console.WriteLine (a * 2)); // And here's what I'm trying to do to invoke it expression.Compile().DynamicInvoke(6); This works fine in the iOS Simulator, the result "12" is printed in my console. But then I tried it on an iPad, and I received the following exception. Object reference not set to an instance of

Learning expression trees in LINQ [closed]

只谈情不闲聊 提交于 2019-12-03 12:37:10
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . What articles/tutorials can you recommend for LINQ Expression Trees? 回答1: http://blogs.msdn.com/b/charlie/archive/2008/01/31/expression-tree-basics.aspx 回答2: If you grok this Building an IQueryable provider, then you grok System.Linq.Expressions . But you can also start with the simple Expression Tree Basics.

Expression/Statement trees

南楼画角 提交于 2019-12-03 12:24:56
Updated Question Further Down I've been experimenting with expression trees in .NET 4 to generate code at runtime and I've been trying to implement the foreach statement by building an expression tree. In the end, the expression should be able to generate a delegate that does this: Action<IEnumerable<int>> action = source => { var enumerator = source.GetEnumerator(); while(enumerator.MoveNext()) { var i = enumerator.Current; // the body of the foreach that I don't currently have yet } } I've come up with the following helper method that generates a BlockExpression from an IEnumerable: public

Is this is an ExpressionTrees bug? #2

邮差的信 提交于 2019-12-03 12:12:23
Looks like ExpressionTrees compiler should be near with the C# spec in many behaviors, but unlike C# there is no support for conversion from decimal to any enum-type : using System; using System.Linq.Expressions; class Program { static void Main() { Func<decimal, ConsoleColor> converter1 = x => (ConsoleColor) x; ConsoleColor c1 = converter1(7m); // fine Expression<Func<decimal, ConsoleColor>> expr = x => (ConsoleColor) x; // System.InvalidOperationException was unhandled // No coercion operator is defined between types // 'System.Decimal' and 'System.ConsoleColor'. Func<decimal, ConsoleColor>

C# switch in lambda expression

痴心易碎 提交于 2019-12-03 11:21:54
问题 Is it possible to have a switch in a lambda expression? If not, why? Resharper displays it as an error. 回答1: You can in a statement block lambda: Action<int> action = x => { switch(x) { case 0: Console.WriteLine("0"); break; default: Console.WriteLine("Not 0"); break; } }; But you can't do it in a "single expression lambda", so this is invalid: // This won't work Expression<Func<int, int>> action = x => switch(x) { case 0: return 0; default: return x + 1; }; This means you can't use switch in