Eval function

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

Hi,

Is there a function like JavaScript eval("2 + 2") function
in .NET?
Something like
Eval(s as string) as object.

Thanks
Mark
 
Check out following article
http://www.microsoft.com/belux/nl/msdn/community/columns/jtielens/datagrid.mspx
The first problem that needs to be solved is: how can we evaluate
expressions, which are stored in a string value at runtime? The first
thought could be: we have to build an interpreter that splits the string
into pieces that we can execute one by one. But this approach would be quite
difficult to build, especially if you want to support a lot of functions.
For example imagine to support ToString, Length, ToUpper, ... But luckily
there is the CodeDom in the .NET Framework that is just what we need!
CodeDom stands for Code Document Object Model, and like what an XMLDocument
is for XML, the CodeDom is for code. With the CodeDom you can describe code
in an object oriented way, like an XMLDocument is a way to describe XML in
an object oriented way. Each code structure has its own class in the
CodeDom, so it's possible to build language-independent code at run-time.
That's right, CodeDom is language-independent, and if you think about it,
it's quite normal: each language has its If-Then, For-Each, ... structures.
Once you have a built code with the CodeDom you can do two things with it:
code generation or compilation. Because the CodeDom is language independent
it's quite easy to generate code for any language. But code compilation is
what we need, we want to be able to execute an expression and get the return
value.

The ExpressionEngine will build at run-time a class that can evaluate an
expression. This expression will be using, in our example, an instance of
the Address class to evaluate. So there will be one function in that class
that takes as a parameter the object we want to evaluate. The return value
of that function will be the result of the expression evaluation.
 
Back
Top