Comments (3)
You cannot access static methods / properties directly in the expression, this is by design (to guarantee that expressions may use only explicitly defined context).
It is possible to expose DateTime.Now in the following way:
varContext["DateTimeNow"] = (Func<DateTime>)(() => DateTime.Now);
Console.WriteLine(lambdaParser.Eval("DateTimeNow()", varContext));
another approach: usage of special 'API' objects in the context
public class DateTimeApi {
public DateTime Now => DateTime.Now;
}
varContext["DateTime"] = new DateTimeApi();
Console.WriteLine(lambdaParser.Eval("DateTime.Now", varContext));
from lambdaparser.
Thanks. If I were to something like this, would it work?
varContext["DateTime"] = (Func<DateTime>)(() => DateTime);
Console.WriteLine(lambdaParser.Eval("DateTime.Now", varContext));
Console.WriteLine(lambdaParser.Eval("DateTime.UtcNow", varContext));
If so, it would be possible to give more static class contexts. I understand that "DateTime.Now()" is needed instead of "DateTime.Now", but is it possible to do this some other way?
I ask this because I tried DynamicExpresso and Flee, but both of them were not able to do what I wanted. I assigned a=1, b=2, c=a+b and then d = c+a. DynamicExpresso and Flee evaluated d to a+b1 instead of 4. But lambdaparser did it correctly so I tried more examples but it failed when I tested the DateTime.
from lambdaparser.
varContext["DateTime"] = (Func)(() => DateTime);
this will not work because DateTime refers to System.Type. You need to pass an object that has properties/methods. You may use an approach with DateTimeApi to enable a syntax similar to C# (like "DateTime.Now") and add to DateTimeApi any properties/methods you want to expose for expressions.
from lambdaparser.
Related Issues (20)
- System.PlatformNotSupportedException: Operation is not supported on this platform. HOT 5
- escaping quotes in nested strings HOT 2
- LambdaParser.Parse HOT 1
- Linq Functions Suppport HOT 2
- SL5 MacOs support HOT 3
- nested expression in dictionary indexer
- Security? HOT 7
- Allow recursivity HOT 5
- Math Operations like power (exponentiate) and to extract a root -possible? HOT 2
- Error - Filter Datatable with LINQ inside the .Eval function HOT 4
- Lambda Sum or Select HOT 1
- Changing order of parameters in simple expression produces different evaluation result. HOT 5
- Expressions using "not" not always work HOT 4
- Support for dynamic types like expando object
- Parser Context Security HOT 1
- "Expected value" error in simple expression HOT 2
- Non-convertible types support
- Variables in expressions HOT 1
- Sequence of ternary operators HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lambdaparser.