zzzprojects / Eval-Expression.NET

C# Eval Expression | Evaluate, Compile, and Execute C# code and expression at runtime.
https://eval-expression.net/
Other
459 stars 86 forks source link

Can't access System.Math Library within expression when application built in release mode .NET MAUI #160

Closed OliverGasbot closed 3 months ago

OliverGasbot commented 3 months ago

We are currently using your library to evaluate various math formula at runtime, these formula are shared across various platforms. Working perfectly in our backend C# API.

Currently developing a .NET MAUI application that evaluates these same formula and while any expressions evaluate perfectly in debug mode, when we build the application in release mode any formula that accesses the System.Math library starts failing (iOS and Android, windows untested).

Even a basic Eval.Execute<double>("double result; result = Math.PI + 1; return result;") fails with the error Oops! The current expression is null at .(Expression , SyntaxNode )

We were hoping for insight as to how we might get around this issue without having to rewrite all our formulas.

JonathanMagnan commented 3 months ago

Hello @OliverGasbot ,

Which version are you using? On our side, we started to support Maui since v5.0.5 (release in 2023)

I just did a test with Android, and everything works on my end.

Do you think you could create a runnable project with the issue? It doesn’t need to be your project, just a new solution with the minimum code (like exactly your basic line) to reproduce the issue.

We do not develop with Maui, so perhaps some configuration is missing on our side, which could explain why we do not get the same error as you.

Best Regards,

Jon

OliverGasbot commented 3 months ago

https://github.com/OliverGasbot/EvalMauiBugRepro

I was able to reproduce using this example built from the maui template, note that if you run the project in "debug" configuration and you press the button to trigger the eval it works, however if you change config to "Release" and run without debugging it will error, tested on Pixel 8 physical and Pixel 5 Simulator

OliverGasbot commented 3 months ago

Turns out this was an issue with MAUI, the linker stripping out parts of the System.Math library, my attempts at stopping that from happening in the project settings were not working, however giving the class calling the eval function the attribute

[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(System.Math))]

Resolved the issue and got the function working in release mode. Thanks for looking into it anyway