Expression evaluation is a very interesting technical topic. There are many technical staff and Implementation Solutions discussing this topic at home and abroad. This is not to say that this problem is difficult to solve, but that it provides a good topic for experts to use their own means to solve the problem. This topic also provides an ideal space for everyone to discuss technical discussions. It is also a classic educational case in the data structure tutorial.
Stack, Expression Tree (. net), but if you want to implement more complex expressions, such as data Method Evaluation, String, whether to evaluate, method calculation, and so on, it is not a teaching case. It should be a compiler problem. Compile a compiler to calculate the value of an expression. It seems that this is not that simple, and it is not a normal technical activity.
How to solve complex expressionsFortunately,. Net already has an interface for dynamic compilation, that is, during the runtime, a bunch of characters are directly compiled into machine code and can be run. This provides a means to evaluate complex expressions .. NET provides the system. codedom namespace, which contains multiple compilation-related classes. These classes can be used for dynamic compilation and execution. However, this compilation process is the same as that of vs2008. If the code is compiled in a simple manner, it is fast. If the code is complex, it is slow and the generated content is also slow. This is because. Net is a pre-compiled mechanism that can be called or computed only after compilation. If you need to compile and calculate immediately, the running efficiency will not be high. The operation efficiency is not high. It cannot be ignored in large-scale and high-concurrency operations, but if only a small amount of computing is used or client resources are used for computing, dynamic compilation is also a very reliable method without affecting the server performance. You can implement any function in the code. net APIs, methods, and classes can be used, and the compilation and interpretation engine is very reliable, which is really a rare good thing. If the content you want to dynamically compile is fixed, rather than the expression that changes every time, it is better to cache the compiled classes and methods, you can use it directly next time, with the same efficiency as writing code. Use. NET Dynamic compiler to explain the calculation formula, is a feasible, stable, reliable expression evaluation solution, but if you need to directly use. NET Dynamic compiler, but also to generate a class, write a part of the Code, such as using, class, and so on, but also have a certain degree of scalability, then not necessarily everyone wants to do that much. Ckrule can greatly simplify this process.
| Serial number |
Requirements |
Ckrule usage |
| 1 |
No fixed object, method, as long as the expression is interpreted |
Create a rule package *. CKP and directly input the expression parameters. |
| 2 |
Requires a fixed object, built-in method |
Add these methods to the new rule set and call them directly. |
Evaluate expressions in your program
Program running:
Code implementation: If (string. isnullorempty (txtword. Text. Trim () {MessageBox. Show ("the script content cannot be blank! ");} Try {VaR _ result = new rulefacade (). testpool <Object> (frmrulepoolset. ruleinstname, txtword. text); txtresult. TEXT = "Result:" + _ result;} catch (exception exp) {MessageBox. show (exp. message);} Only uses one of the simplest interfaces, the testpool method of the rulefacade class. You can use a wide array of expressions to evaluate the Rule Set Name and expression content. But as mentioned earlier ,. NET Dynamic compilation mechanism, destined for this dynamic evaluation solution is not very suitable for large-scale high concurrency computing, however, if you use the client's desktop resources or do not have high concurrency requirements, it is still very useful. Source code download: http://www.ckrule.com/cn/demo.html