-
-
Notifications
You must be signed in to change notification settings - Fork 231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for parsing Action lambdas #425
Comments
Hello @glopesdev , Thank you for reporting, we will look at it if something already similar already exists. Best Regards, Jon Performance Libraries Runtime Evaluation |
Hello @glopesdev , The v1.2.4 has been finally released. It's now possible to pass the delegate type for most method such as: var expression = (Action<int>)DynamicExpressionParser.ParseLambda(typeof(Action<int>), new[] { Expression.Parameter(typeof(int), "x") }, typeof(int), "x + 1").Compile();
var expression2 = (Func<int, int>)DynamicExpressionParser.ParseLambda(typeof(Func<int, int>), new[] { Expression.Parameter(typeof(int), "x") }, typeof(int), "x + 1").Compile();
var r1 = expression2(3); Let me know if that's exactly what you were looking for. Best Regards, Jon |
@JonathanMagnan that is exactly what I was hoping for, thanks a lot for the support, now I can finally jump over from the old |
Awesome @glopesdev We are glad we sorted things out ;) Don't hesitate to contact us for any questions, issues or feedback! Best regards, Jon |
I am trying something like this:
however, the value is not changed to "123"... do you have any ideas? |
Hello @niuzheng168 , Do you think you could provide a runnable project sample for this issue? It will help my developer to get started to investigate it faster and make sure nothing is missing. Providing a project sample is now REQUIRED. It happened too many times that something was missing to investigate and/or answer an issue. Try to create a new project with only the minimal code (having too many non-related codes doesn’t help either). You can send it to [email protected] if you need to keep the source private Best Regards, Jon Performance Libraries Runtime Evaluation |
@niuzheng168 @JonathanMagnan I don't know if this is the same issue, but I'm also testing this new feature and I found a parse limitation with the following code sample: using System;
using System.Collections.Generic;
using System.Linq.Dynamic.Core;
using System.Linq.Dynamic.Core.CustomTypeProviders;
using System.Linq.Expressions;
namespace TestExpressions
{
class Switch
{
public void Press()
{
Console.WriteLine("!");
}
}
class TypeProvider : DefaultDynamicLinqCustomTypeProvider
{
public override HashSet<Type> GetCustomTypes()
{
var customTypes = base.GetCustomTypes();
customTypes.Add(typeof(Switch));
return customTypes;
}
}
class Program
{
static void Main(string[] args)
{
var s = new Switch();
var parsing = new ParsingConfig();
parsing.CustomTypeProvider = new TypeProvider();
var parameter = Expression.Parameter(typeof(Switch), "it");
// the following line will throw a System.Linq.Dynamic.Core.Exceptions.ParseException:
// Method 'Press' in type 'Switch' does not return a value'
var lambda = (Action<Switch>)DynamicExpressionParser.ParseLambda(typeof(Action<Switch>), parsing, new[] { parameter }, null, "it.Press()").Compile();
lambda(s);
}
}
} This seems to boil down to L1656 on if (method.ReturnType == typeof(void))
{
throw ParseError(errorPos, Res.MethodIsVoid, id, TypeHelper.GetTypeName(method.DeclaringType));
} The check makes sense if we expect only Removing the check is not a problem since |
@JonathanMagnan try below codes: using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
namespace DynamicExpression
{
public class ListItem
{
public int Id { get; set; }
public string Value { get; set; }
}
public class NestedInput
{
public string SimpleProp { get; set; }
public List<ListItem> NestedProp { get; set; }
}
class Program
{
static NestedInput nestedInput = new NestedInput
{
SimpleProp = "simpleProp",
NestedProp = new List<ListItem>
{
new ListItem
{
Id = 1,
Value = "first"
}
}
};
static void Main(string[] args)
{
var expression = (Action<NestedInput, int>)DynamicExpressionParser.ParseLambda(
typeof(Action<NestedInput, int>),
new[] { Expression.Parameter(typeof(NestedInput), "x"), Expression.Parameter(typeof(int), "y") },
null,
"x.NestedProp[0].Id = y").Compile();
expression.Invoke(nestedInput, 10);
Console.WriteLine(nestedInput.NestedProp[0].Id);
}
}
} I suppose the it will nestedInput.NestedProp[0].Id will set to 10, but it still 1 after expression executed. |
This was possible in the old
System.Linq.Dynamic
package simply by allowing the specification of the delegate type passed toExpression.Lambda
from one of the overloads inParseLambda
(see archived source).This would greatly increase the flexibility of the dynamic linq library for composing arbitrary statement blocks.
The text was updated successfully, but these errors were encountered: