-
-
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
Question: Generic and Param based Custom Type Methods Support #386
Comments
Hello @joshidp , Depending on your requirement, I might recommend you our other library instead: https://eval-expression.net/ This library is not free (unless you use only the LINQ part), but allow you to do pretty much anything such as using static method that you have created (You will need to notify our library by Registering the type). If the only method not supported you want is creating an array, we might add this in this library as we see a value here. However, if you want to go a lot deeper than this library Let me know if all you are looking for is creating an array or not. Best Regards, Jon Performance Libraries Runtime Evaluation |
@joshidp This is possible. You need to create a static class and a static method. And annotate that class with the DynamicLinqType attribute. Utils class[DynamicLinqType]
public static class Utils
{
public static string[] ConvertToArray(string[] values)
{
if (values == null)
{
return new string[0];
}
return values.ToArray();
}
} Usagepublic class X
{
public string[] Values { get; set; }
}
var list = new[] { new X { }, new X { Values = new[] { "a", "b" } } }.AsQueryable();
var result = list.Select(config, "Utils.ConvertToArray(Values)").ToDynamicList<string[]>(); However, these helper methods can only work on Linq to Objects. Please let me know if this works for you. |
Thanks @StefH for the reply. If I use your example of Class X the solution works but if I try to use my example I still get an error "System.Linq.Dynamic.Core.Exceptions.ParseException: 'No applicable method 'ConvertToArray' exists in type 'Utils'' In you example, Values prop is already a string array but what I am trying to do is pass string param and get the array back. Show below Utility class
Usage
Please tell how this issue can be resolved. Thanks! in advance. |
I think the params is not supported, so you can only use |
ok, Thanks. For now I'm thinking to write overloaded methods to return string[], it should suffice for limited set of values.
Thanks |
Hello @joshidp , Thank @StefH for pointing us out the The v1.1.3 has been released. The library should now support Let us know if everything works as expected. |
I think that the original method from @joshidp is still not recognized? public static string[] ConvertToArray(params string[] values)
{
if (values == null)
{
return new string[0];
}
return values.ToArray();
} With public class X
{
public string[] Values { get; set; }
}
var list = new[] { new X { }, new X { Values = new[] { "a", "b" } } }.AsQueryable();
var result = list.Select(config, "Utils.ConvertToArray(Values)").ToDynamicList<string[]>(); Still throws:
|
You are right, we forget probably the most essential case! The v1.1.5 has been released |
@StefH @JonathanMagnan |
Hello @JimmyLzy , When you say Unless I'm wrong, extension methods are not yet supported. We will look if that's possible or not to support basic scenario at least. |
@JonathanMagnan |
What do you mean @JimmyLzy by Unless I'm wrong, the library doesn't support yet extension methods when I checked yesterday. |
@JonathanMagnan I meant running code in my windows laptop |
@JonathanMagnan I managed to get more exception message Expression expected (at index 41). Actually the issue is on our side. I forgot to clear some cache which causes the predicate to be generated incorrectly. It was Executing query field = @0 AND (FilterUtils.WithinXMiles(,@1,@2)) with args GeoCoord,0 0,20. That's why the library can not parse it. |
Hello @JimmyLzy , Just to let you know that a first version to support the extension method has been made. We are currently completing the testing phase and a new version should be available within a few days. |
Hello @JimmyLzy , The v1.1.6 has been released which should now support extension methods. See those unit tests example: https://github.com/zzzprojects/System.Linq.Dynamic.Core/blob/master/test/System.Linq.Dynamic.Core.Tests/Parser/DynamicLinqTypeTest.cs#L30 Let me know if everything works as expected. |
@JonathanMagnan i think it does not work, if extension uses generics. |
You are right @Feroks , We did it very basic for the first version. We support some simple scenarios. I'm currently not sure if we wish to go further and support more scenario or redirect people to our other library C# Eval Expression which handle more complex overload resolution. If you wish we try it, create a new issue with an example of a method and I will ask my developer to look at it. I believe it could be possible to handle one generic parameter (which is more than enough for a lot of cases) but supporting multiple generic parameters would be too long for now. |
… methods (related to zzzprojects#386 )
… methods (related to zzzprojects#386 )
* support for generic (static) methods, thus also support for extension methods (related to zzzprojects#386 ) * support for generic (static) methods, thus also support for extension methods (related to zzzprojects#386 )
Hello,
Thanks for the awesome work.
I'm using your lib to write json configurable rules. I want to used generic and param based custom type methods as Utility methods. Below is a sample json config snippet.
{"expression": "Utilities.ConvertToArray(\"xyz\",\"abc\")"}
and below is the method with param
When I try to call DynamicExpressionParser.ParseLambda method with above expression it fails with an error saying "No method called ConvertToArray found in Utilities". Please not normal method without param works just fine.
I want to create an array but since that was not possible in the expression directly using your lib I tried using the Utility method.
Similarly, generic methods also doesn't work.
Please help in providing a solution.
Thanks
The text was updated successfully, but these errors were encountered: