Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

I need an evaluator for string expressions in C#. After searching around, i came to the conclusion that there are only two methods (which i could understand :P) to create an evaluator: using reflection and by using JavaScript. Which one is better? is there another method which is easier and better than these two methods?

Hoping you guys can help me out

Thank you
Posted
Comments
luisnike19 1-Apr-11 0:54am    
Why do you think u need reflection?
what about Regular expressions?
aeonjake 1-Apr-11 7:28am    
i need a expression evaluator in the sense 2+3 should give 5. to evaluate a code as such i can recompile the expression '2+3' and i get the result (using the evaluator given on code project) http://www.codeproject.com/KB/cs/runtime_eval.aspx
Perry Bruins 1-Apr-11 0:57am    
What kind of string expressions do you need to evaluate?
aeonjake 1-Apr-11 7:28am    
i need a expression evaluator in the sense 2+3 should give 5. to evaluate a code as such i can recompile the expression '2+3' and i get the result (using the evaluator given on code project)http://www.codeproject.com/KB/cs/runtime_eval.aspx

1 solution

None. You can use C# as input language, compile it using CodeDom and run. The latter is a bit difficult as you need to do all in a separate Application Domain, but the ultimate value is: you don't use any 3rd part code and use available software which is compliant with ECMA and ISO standard.

Here is the skeleton of such application I depicted:
Create WPF Application that uses Reloadable Plugins...[^]
code generating using CodeDom[^]

To confuse you some more, here is my Javascript-only calculator: http://www.sakryukov.org/freeware/calculator/[^]. This is just one file, you can copy and use if.

In both cases, the same idea: using the language (with run-time library) capable of compiling or interpreting code on the same language. In case of .NET, it can be any of standard .NET languages as well.

—SA
 
Share this answer
 
v2
Comments
aeonjake 1-Apr-11 7:38am    
So, stick to C# code and not JavaScript? i came across the Flee evaluator also. should i use this?
Sergey Alexandrovich Kryukov 1-Apr-11 11:23am    
I updated the Answer; see my ready-to-use Javascript calculator as well. Same idea, but much more simple. Can you get the idea: not using any of those "evaluators", using available language, "self-compilation" if you will.
--SA
aeonjake 1-Apr-11 15:43pm    
I already have started using the js evaluator but i will keep in mind what you have said
Sergey Alexandrovich Kryukov 1-Apr-11 16:08pm    
With C# evaluator makes some sense, but with Javascript it is readily evailable already, nothing to build at all.

Look at my Javascript code (last reference):

outs = eval(expr);

The function "eval" is not mine! It is already available from Javascript runtime.
Do you see it now? It it ***already*** done. Don't waste your time!

--SA


Albin Abel 1-Apr-11 12:55pm    
Good options. My 5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900