How to create formulas at runtime in C #?

3

There are many advantages when things change for the better, so were the changes from Clipper to Delphi and C #, in fact just the visual part. There was in Clipper a fantastic feature called MACRO SUBSTITUTION.

Well, in Clipper it was possible to create formulas using Macro Replacement, where the user could create them or use the patterns that were stored in the database.

A simple example would be the calculation of an individual's salary, as follows:

{
    var SB = 1200,00;
    var HM = 220;
    var HT = 215;
    var VP = 0;

    var sFormula = 'HT * (SB / HM)';

    VP = &sFormula;
}

Where:   - SB = Base Salary   - HM = Monthly Hours   - HT = Hours worked   - VP = Payment Amount

As SB, HM and HT can change from employee to employee, these would be values received in a function that would return the value of VP that would be 1172.73; This same process for calculating all events on the Payroll.

Because this method, because it gives the Company freedom to create any formula that it thinks necessary to make some calculation, the only limitation would be the use of the acronyms used in the formulas because they are defined by the software, however to include more acronyms would be a process simple.

I know this is possible in PHP and I need to know if anyone knows how to do it in C #.

    
asked by anonymous 19.01.2016 / 23:35

1 answer

5

Macro abuse

The use of macros in Clipper was a great gambiarra that had several problems and was frequently abused, as is the case of the example shown.

There is no reason to do this other than ignorance or laziness to do it right. Most of the places where macro was used could be done without the macro in a simpler and certainly safer way. The case of compiling an arbitrary formula was a case that might be justifiable. But it is not. Even the Harbor has a better solution than Clipper, compiling it right and saving and loading blocks of compiled code.

Suppose you really want to let users type formulas on their own. This is foolhardy and should be accompanied by a compiler itself to make sure everything is ok and is not doing anything more than it should. The use of the macro is a huge security hole. For those who do not know what it is and are wondering what that would be, it is eval of JavaScript.

C # Equivalent

In static and fully compiled languages it is more complicated to do this. C # even provides some ways to run in a virtual machine (it would also give a more complete runtime ). But before doing this, try to use the right tools of a static language. Find out about the structural structural design patterns , especially the inStrategy pattern that I think is what I really need. It will not allow the user to enter formulas but will facilitate the inclusion of formulas to customize the software. These formulas will be previously compiled and possibly validated by the software vendor. There are several techniques to follow to create a secure plugin system that performs this properly.

Another possibility to actually let the user enter the formula is to create a compiler for this.

OC # always allowed code generation at runtime, but it was complicated, with .Net Compiler Platform was much easier. It even gives you the ability to simulate what the Clipper macro did in a reasonably simple way, and if you abstract, the syntax is even similar. But the fact that you can generate code from a source does not mean that you have to do it. It does not mean that a number of safety measures should not be taken. And they are reasonably complicated measures to implement.

An example of the scripting API:

int result = await CSharpScript.EvaluateAsync<int>("1 + 2");

An introduction to documentation can be found on Microsoft's GitHub. There is no definitive official documentation yet.

I find Async unnecessary in simple cases like this, but I do not even know if the current version has a synchronous execution.

Conclusion

I should stress that this should not be used by those who do not have full understanding of all the implications of their use and know how to solve everything or accept that code is reckless. Should have a law enforcing a code so can only be used with express consent of the user. While it is true that a user who is harmed by this and has not been notified of the risk (in some cases even if it has been notified), it can certainly prosecute those who exposed it in this way.

    
20.01.2016 / 00:03