2

Possible Duplicate:
using eval in Java

In javascript i can call a function using eval like :

eval('my_javascript_code;')

Is there anything like that in C# or JAVA which lets me run C# or JAVA code?

String time = Eval("My csharp code");

Or Am I completely off the track?

Community
  • 1
  • 1
Akhil Sekharan
  • 12,467
  • 7
  • 40
  • 57
  • You can use [Beanshell](http://www.beanshell.org/) in Java. This produces a scripting library for Java and is used in many debuggers for ad hoc expressions. However, use of "eval" or dynamic code is discouraged in Java (and I suspect most languages it's not a good idea) – Peter Lawrey Dec 05 '12 at 10:19

1 Answers1

0

There is no direct analogue - there is no built in function that will take a string and run it as executable code.

You can use the different reflection options of either platform to execute code dynamically. These include creating classes in code, compiling them and executing them and is not easy or fast.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 1
    Not easy or fast indeed. Last I tried this in C#, it took over 100 milliseconds to execute a trivial arithmetic operation in this fashion. – Deestan Dec 05 '12 at 10:09
  • @Downvoter - care to comment? – Oded Dec 05 '12 at 11:14
  • 1
    Not my downvote, but I guess the answer was a bit vague. Maybe add some links, or searchable terms like "runtime compiling" and "dynamic assembly loading"? – Deestan Dec 05 '12 at 11:20