Open OPunktSchmidt opened 4 years ago
I have created a very minimalistic repository to reproduce the error on ios. It is an Xamarin.Forms project. When you start the iOs Project a exception is thrown, on Android not.
https://github.com/OPunktSchmidt/NRecoLambdaParser.Issue21.XamarinForms
This is the relevant test code. It is placed in App.xaml.cs and is exceuted immediately on when starting the project.
protected override void OnStart()
{
// Handle when your app starts
try
{
NReco.Linq.LambdaParser parser = new NReco.Linq.LambdaParser(new NReco.Linq.ValueComparer() { NullComparison = NReco.Linq.ValueComparer.NullComparisonMode.Sql });
string evalString = "(val1.Contains(val2) || val3.Contains(val4) || val5.Contains(val6) || val7.Contains(val8) || val9.Contains(val10) || val11.Contains(val12) || val13.Contains(val14) || val15.Contains(val16) || val17.Contains(val18) || val19.Contains(val20))";
System.Collections.Generic.Dictionary<string, object> values = new System.Collections.Generic.Dictionary<string, object>();
values.Add("val20", "10;");
values.Add("val17", "2;4;5;7;8;");
values.Add("val12", "6");
values.Add("val7", "2;4;5;7;8;");
values.Add("val2", "1");
values.Add("val19", "2;4;5;7;8;");
values.Add("val13", "2;4;5;7;8;");
values.Add("val8", "4");
values.Add("val3", "2;4;5;7;8;");
values.Add("val1", "2;4;5;7;8;");
values.Add("val5", "2;4;5;7;8;");
values.Add("val16", "8");
values.Add("val18", "9");
values.Add("val14", "7");
values.Add("val4", "2");
values.Add("val11", "2;4;5;7;8;");
values.Add("val9", "2;4;5;7;8;");
values.Add("val10", "5");
values.Add("val15", "2;4;5;7;8;");
values.Add("val6", "3");
object evalResult = parser.Eval(evalString, values);
System.Diagnostics.Debugger.Break();
}
catch (System.Exception ex)
{
System.Diagnostics.Debugger.Break();
}
}
Oliver, do you have any idea how to fix this incompatibility on iOS? I don't have enough expertise on .net+mobile platform. It seems LambdaParser tries to create a delegate that doesn't available on ios (Func<>?).
I haven't had a closer look. But in this depth I don't have much expertise in mobile development either.
I'll take a closer look at it. I'll be on vacation next week but I'll definitely get closer to it when I'm back in the office.
I'll keep you informed.
I did some research.
"Since the iOS kernel prevents an application from generating code dynamically, Xamarin.iOS does not support any form of dynamic code generation." (https://docs.microsoft.com/bg-bg/xamarin/ios/internals/limitations)
Simple expressions like "val1 == val2" or "val1 > val2" seems to work on iOs, but more complex expressions like mine above not (there is a methode-call to "Contains in the expression).
Maybe the method-call to "Contains" requires something from namespace System.Reflection.Emit? System.Reflection.Emit is not available due to limitations under ios. I haven't worked much with dynamic code generation and System.Linq.Expressions so far so I can't say much about it.
Here are a few more interesting links:
https://stackoverflow.com/questions/19666257/how-to-compile-methodcallexpression-without-dynamicinvoke https://stackoverflow.com/questions/24977939/what-does-expression-compile-do-on-monotouch https://stackoverflow.com/questions/29245589/why-does-lambdaexpression-compile-work-on-ios-xamarin
I'll keep investigating...
According to this documentation page, dynamic code generation is not supported on iOS; from the other side, NReco.LambdaParser uses only System.Expressions which should fallback to 'interpretation' mode on iOS. You reported that "val1 == val2" or "val1 > val2" work fine, so I may assume that an error may be caused by a bug related to the MethodCallExpression 'interpretation'.
First of all, it is good idea to test expression like "val3.Contains(val4)" with latest Xamarin platform, maybe this issue is already solved. If this doesn't help, you can modify LambdaParser class to use Expression.Compile(true) to force interpretation mode - who knows maybe this is needed for iOS.
Finally, execution of expressions on iOS 100% might be possible with own LambdaParser 'interpretation' (however this will require some development + I assume evaluation performance may be not so good).
We are getting this Exception on iOs. On Android it works perfectly.
This is the expression string:
(val1.Contains(val2) || val3.Contains(val4) || val5.Contains(val6) || val7.Contains(val8) || val9.Contains(val10) || val11.Contains(val12) || val13.Contains(val14) || val15.Contains(val16) || val17.Contains(val18) || val19.Contains(val20))
And the values: