Git Product home page Git Product logo

Comments (7)

VitaliyMF avatar VitaliyMF commented on May 27, 2024

LambdaParser.Parse parses an expression into .NET Expression, and you don't get any errors if it is syntactically correct.

dt is a System.DataTable type variable,Rows1 is an incorrect property,But it didn't give me any exceptions,i need some checks,how to do?

It is possible to determine that "Rows1" is incorrect property only when your expression is evaluated (for the concrete evaluation context with contains concrete "dt" instance), and you'll get an error when you'll call "Eval" for this expression and the evaluation context.

from lambdaparser.

DNetORM avatar DNetORM commented on May 27, 2024

Is it possible to do this? Provide a dictionary that stores types with the same key values, and then perform syntax checks based on this dictionary, such as varTypeContext

var varContext = new Dictionary<string, object>();
varContext["dt"] = datatable;

var varTypeContext = new Dictionary<string, object>();
varTypeContext ["dt"] = "System.DataTable";

bool res = lambdaParser.CheckExpression("dt.Rows1[0]");

from lambdaparser.

DNetORM avatar DNetORM commented on May 27, 2024

I am developing a simple workflow engine and need to define some variables in advance. These variables may have mutual usage relationships, but when defining variables, I do not know the specific values of the variables. I just want to check the syntax of the expression because some variables will only be instantiated at runtime, so I cannot use eval() at this stage

from lambdaparser.

VitaliyMF avatar VitaliyMF commented on May 27, 2024

Is it possible to do this? Provide a dictionary that stores types with the same key values, and then perform syntax checks based on this dictionary, such as varTypeContext
bool res = lambdaParser.CheckExpression("dt.Rows1[0]");

Internal behavior of "CheckExpression" is equivalent to

var res = true;
try {
  lambdaParser.Eval("dt.Rows1[0]", varContext);
} catch (Exception ex) {
  res = false;
}

(note that it is not possible to check an expression for run-time errors without the evaluation context and performing actual evaluation).

Also, if an expression has conditions, this kind of check can verify only correctness for the concrete evaluation context. It is not possible to verify an expression for runtime errors in another way (in the same way as you cannot check for JS runtime errors just by parsing JS code).

I don't see a real need in this API inside LambdaParser because handling of errors may be rather application-specific.

from lambdaparser.

VitaliyMF avatar VitaliyMF commented on May 27, 2024

but when defining variables, I do not know the specific values of the variables. I just want to check the syntax of the expression because some variables will only be instantiated at runtime, so I cannot use eval() at this stage

This means that you can validate expression's syntax with Parse method, but you'll not able to detect runtime errors that depend on the concrete evaluation context. An expression "dt.Rows1[0]" can be valid in one case (if "dt" variable is set and it has "Rows1" property) and can cause an error in anther case. Without evaluation with concrete "dt" value, you cannot validate that (this is a conceptual limitation, not technical).

from lambdaparser.

DNetORM avatar DNetORM commented on May 27, 2024

I have changed my approach and used Rosslyn CSharpCompilation to check for syntax issues in compilation. Since Rosslyn can do it, technically your project should also be able to implement it. I think it's just whether you're willing to think about improving it or not

from lambdaparser.

VitaliyMF avatar VitaliyMF commented on May 27, 2024

Since Rosslyn can do it, technically your project should also be able to implement it.

You're wrong with this assumption. NReco.LambdaParser was specially designed for dynamic typing, it implements special mechanism to support this capability: you are not obligated explicitly define variables and their (.net) types, and this means that all methods/properties are resolved in the run-time (via reflection). A side effect of dynamic typing is that you cannot validate types just by parsing the expression, this is conceptual difference from Roslyn (or any other parsing of strictly-typed expressions).

You may check LambdaParser code to realize that it is not possible to handle type-related errors (like calling wrong method or property) simply because when you have "dt" variable (ParameterExpression) it has System.Object type and parser simply don't have information about its real type - it is known only in the run-time, when an expression is evaluated for the concrete variables context.

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.