Dynamic Expression API (Dynamic.cs) Not properly parsing expression in .net 3.5
I'm having trouble getting this API working in .net 3.5 (works fine in 4.0). Basically I have following code
List<ParameterExpression> parameters = new List<ParameterExpression&开发者_如何学Pythongt;();
parameters.Add(Expression.Parameter(typeof(double), "R0C6"));
parameters.Add(Expression.Parameter(typeof(double), "R0C7"));
parameters.Add(Expression.Parameter(typeof(double), "R0C8"));
parameters.Add(Expression.Parameter(typeof(double), "R0C9"));
parameters.Add(Expression.Parameter(typeof(double), "R0C10"));
parameters.Add(Expression.Parameter(typeof(double), "R0C11"));
parameters.Add(Expression.Parameter(typeof(double), "R0C12"));
LambdaExpression e = DynamicExpressionBuilder.ParseLambda(
parameters.ToArray(), typeof(double), "R0C6 + R0C7 + R0C8 + R0C9 + R0C10 + R0C11 + R0C12");
var result = e.Compile().DynamicInvoke(1, 1, 1, 1, 1, 1, 1);
Console.WriteLine(result);
When running this code I get ArgumentException. The reason being .NET 3.5 does not have Func delegate which takes more than 4 parameters. Can someone suggest me change in Dynamic.cs so that I can get able code working in 3.5?
Yes thats one alternative. Here is the code that worked for me
List<ParameterExpression> parameters = new List<ParameterExpression>();
parameters.Add(Expression.Parameter(typeof(Holder), "H"));
LambdaExpression e = DynamicExpressionBuilder.ParseLambda(parameters.ToArray(), typeof(double), "H.R0C6 + H.R0C7 + H.R0C8 + H.R0C9 + H.R0C10 + H.R0C11 + H.R0C12");
var result = e.Compile().DynamicInvoke(new Holder { R0C6 = 1, R0C7 = 1, R0C8 = 1, R0C9 = 1, R0C10 = 1, R0C11 = 1, R0C12 = 1 });
Console.WriteLine(result);
But surely there should be better way of achieving my original implementation.
Is there a reason that you can't combine all the doubles into a single parameter object that contains fields or properties for the values? Then you'd only need one parameter in your Func for the expression.
精彩评论