Git Product home page Git Product logo

Comments (3)

VitaliyMF avatar VitaliyMF commented on May 27, 2024

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.

trrahul avatar trrahul commented on May 27, 2024

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.

VitaliyMF avatar VitaliyMF commented on May 27, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.