
Eval function in VB .Net and C#
Patrick,
It should be pointed out that this will only evaluate JavaScript code,
it will not evaluate C# and VB.NET code.
Also, I don't know that this capability ^should^ be in the FCL (or CLR).
I mean, it is a compiled language, and if you look at a basic JS assembly,
there is a lot of work going on in there to allow evaluated expressions.
Personally, I am of the belief that whatever needs to be done can be done
with things such as abstract classes, interfaces, and good design.
--
- Nicholas Paldino [.NET/C# MVP]
Quote:
> Hi,
> I have recently seen many questions about the lack of an equivalent to
> the JScript eval() function in C# and VB .Net. This is a good
> opportunity to demonstrate the cross-langauge capabilities of .Net
> 1) Create a file named CustomEval.js (File | New | File | Script) as
> follows:
> // JScript source code
> class CustomEval
> {
> static function eval(strExp)
> {
> return eval(strExp);
> }
> }
> 2) Compile this source code from the command line:
> jsc /t:library CustomEval.js
> which should produce CustomEval.dll
> 3) Reference this assembly from your VB .Net or C# project as well as
> the Microsoft.JScript assembly.
> 4) Use the eval method of CustomEval as follows (since the method is
> static, you don't need to instantiate CustomEval.
> result = CustomEval.eval("2+3")
> Of course, I'd prefer having this capability implemented in the FCL. Or
> did I miss it?
> --
> Patrick Philippot
> MainSoft Consulting Services
> www.mainsoft.xx
> (replace .xx with .fr when replying by e-mail)