eval equivalent

  • Thread starter Thread starter Andrea Williams
  • Start date Start date
A

Andrea Williams

I'm looking for an equivalent to the JavaScript function 'eval'. Anyone
know one that will work in C#? I'm guessing that it doesn't exist due to
strong data typing, but thought I'd ask.

Thx!
Andrea
 
Andrea,

There really isn't anything that will allow you to do this. C# is not
an interpreted language, like JavaScript is. Because of that, there is no
easy eval functionality. You could write the code out yourself, and create
an assembly dynamically, but you would have to use reflection to make the
calls, or have an interface that is defined elsewhere implemented on the
classes you create.

You are better off creating an interface/base class definition and then
having classes derive from that.

Hope this helps.
 
I suspected something like this... Thanks!

Andrea


Nicholas Paldino said:
Andrea,

There really isn't anything that will allow you to do this. C# is not
an interpreted language, like JavaScript is. Because of that, there is no
easy eval functionality. You could write the code out yourself, and create
an assembly dynamically, but you would have to use reflection to make the
calls, or have an interface that is defined elsewhere implemented on the
classes you create.

You are better off creating an interface/base class definition and then
having classes derive from that.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Andrea Williams said:
I'm looking for an equivalent to the JavaScript function 'eval'. Anyone
know one that will work in C#? I'm guessing that it doesn't exist due to
strong data typing, but thought I'd ask.

Thx!
Andrea
 
No C# doesn't have it, but you can always build a managed assembly using
JScript.Net that executes a string passed to a function that executes the
'eval' function.

Willy.
 
Back
Top