expression-trees

How can I create an expression that either throws an exception or returns a value based on a condition?

痴心易碎 提交于 2019-12-10 21:43:37
问题 I'm struggling to build an expression that if the condition is true throws an exception and if it's false that it should return a value but I'm always getting the ArgumentException : var expr = Expression.Condition( Expression.Equal(Expression.Constant(0), Expression.Constant(0)), Expression.Throw(Expression.Constant(new DivideByZeroException())), Expression.Constant(1)); var lambda = Expression.Lambda<Func<int>>(expr); var result = lambda.Compile()(); If I put Expression.Empty() as the third

In C#, how can I create a value type variable at runtime?

不问归期 提交于 2019-12-10 20:44:23
问题 I am attempting to implement a method like: (Func<T> getFn, Action<T> setFn) MakePair<T>(T initialVal) { } It will return two runtime generated lambdas that get and set a dynamically created variable using Expression trees to create the code. My current solution is to dynamically create an array of the type with one element, and reference that: (Func<T> getFn, Action<T> setFn) MakePair<T>(T initialVal) { var dynvar = Array.CreateInstance(typeof(T), 1); Expression<Func<Array>> f = () => dynvar

Dynamic Linq 2 Sql using Expressions Trees raising exception “Binary Operator LessThan not defined for System.String and System.String”

ε祈祈猫儿з 提交于 2019-12-10 19:42:53
问题 I'm trying to write a dynamic Linq 2 Sql query using Expressions trees but I'm getting a exception telling me that the LessThan and GreaterThan operators are not defined for System.String and System.String , which i find odd, is that true? or am I doing something wrong? Expression<Func<SomeDataContextType, string>> codeSelectorExpresion = x => x.CodeColumn; var row = Expression.Parameter(typeof(SomeDataContextType), "row"); var expression = Expression.GreaterThan( Expression.Invoke

Building an OrderBy expression using the name of a property

走远了吗. 提交于 2019-12-10 19:08:57
问题 I'm trying to support sorting via the WebGrid control in MVC3, which passes the name of a property on my model into my actions via a sort parameter. public class Agent { public int Id { get; set; } public string Name { get; set; } } [HttpGet] public ActionResult Index(string sort = "Id", string sortdir = "ASC") { // Define the parameter that we are going to use in the OrderBy clause. var param = Expression.Parameter(typeof(Agent), "agent"); // Now we'll make our lambda function that returns

Creating an expression tree that uses a dynamically generated type

ε祈祈猫儿з 提交于 2019-12-10 17:22:58
问题 I have a fully initialized MethodBuilder and EnumBuilder . The MethodBuilder points to the entry point of a dynamic assembly. It has the following signature: public static int Main (string [] args) {...} The assembly generation code works fine and I can use Reflection.Emit to test it. Instead of emitting IL, I want to save the target code from an expression tree. It should: declare a enum variable assign it a value write to the console read pause the console return a enum value as an Int32

LambdaExpression Variable Referenced From Scope But Not Defined

∥☆過路亽.° 提交于 2019-12-10 16:17:03
问题 I have a simple lambda expression that I would like to compile and invoke Expression< Func< Commands, bool>> expression = c => c.IsValid("test"); but when I do the following: LambdaExpression le = Expression.Lambda(expression.Body); object result = le.Compile().DynamicInvoke(); the compile throws the error: variable 'c' of type 'ConsoleApplication1.Commands' referenced from scope '', but it is not defined How do you set the instance variable for this expression? 回答1: Why not just compile the

Expression.Call and Count

故事扮演 提交于 2019-12-10 16:13:27
问题 I'm looking for a way to do following dynamically: var q = context.Subscription .Include("Client") .Include("Invoices") Where(s=>s.Client.Invoices.Count(i=>i.InvoiceID == SomeInt) > 0); I would like to build expression dynamically for the left side: Expression left = s => s.Client.Invoices.Count(i => i.InvoiceID == iSomeVar); //! Expression right = Expression.Constant(0); var binary = Expression.GreaterThan(left, right); Thanks! UPDATED NOTES: Please note: The end result must be Expression

Create an expression tree in c#

你离开我真会死。 提交于 2019-12-10 14:58:42
问题 I am attempting to create a dynamic query using expression trees in LINQ to represent the following query WageConstIns.Where(WageConstIn => WageConstIn.Serialno.ToString().StartsWith("2800")); I have attempted to create it like so: MemberExpression le1 = LinqExpression.Property(paramExp, "Serialno"); MethodCallExpression le2 = LinqExpression.Call(le1, typeof(string).GetMethod("ToString", System.Type.EmptyTypes)); ConstantExpression le3 = LinqExpression.Constant("2800"); MethodCallExpression

Getting Values from ExpressionTrees

你。 提交于 2019-12-10 13:38:22
问题 let there be: Expression<Func<Customer, bool>> expression = c => c.Name == "John"; now i get the value by using : string myvalue = ((ConstantExpression) bin.Right).Value; now let there be: string x = "John"; Expression<Func<Customer, bool>> expression = c => c.Name == x; now i understand that string myvalue = ((ConstantExpression) bin.Right).Value; would generate an error because the bin.right here is not constantexpression its a field expression but the question is how do i get the value

How can I Create a Expression.Property of a child object

巧了我就是萌 提交于 2019-12-10 13:18:19
问题 normally I create an expresion in this way. ParameterExpression pe = Expression.Parameter(typeof(object1), "x"); string Name = "property1"; MemberExpression left = Expression.Property(pe, (object1).GetProperty(Name)); it produces left = x => x.property1 I need to know how can I produce left = x => x.Object2.property1 if Name = "Object2.property1"; and object2 is a child to object1 Thanks in advance 回答1: I don't quite understand what you want. Is it a property chain (say: x.Prop1.Prop2)? var