0

I wonder if there is some method of executing a string, or convert it to a code, example: "Console.WriteLine (\" Hello \ ")";

Equivalent to 'eval' in JavaScript.

I've translated it from Translate Google.

Translate Google

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user314994
  • 13
  • 1
  • Sidenote: there is a portuguese version of Stack Overflow: http://pt.stackoverflow.com/ – Jeroen Vannevel May 10 '14 at 00:03
  • possible duplicate of [How can I read the properties of a C# class dynamically?](http://stackoverflow.com/questions/4629/how-can-i-read-the-properties-of-a-c-sharp-class-dynamically) – John Saunders May 10 '14 at 00:06
  • possible duplicate of http://stackoverflow.com/questions/12964790/using-c-sharp-as-my-dsl-is-this-possible-if-so-how in which case ChristopheD's answer (below) is best. – philologon May 10 '14 at 07:25

4 Answers4

2

It is a bit more contrived then using eval in some languages, but it is certainly possible to programmatically compile code.

ChristopheD
  • 112,638
  • 29
  • 165
  • 179
0

I wrote a tutorial on something very similar to this a few years ago - it is based on compiling though:

http://domscode.com/2011/02/16/new-tutorial-tdd-c-web-compiler/

Hope that helps

ozidom
  • 159
  • 1
  • 5
0

You can try cshell: http://cshell.net/

It has a repl-based and executes c# code.

deostroll
  • 11,661
  • 21
  • 90
  • 161
0

I haven't tried it myself, but it seems what you're looking for is easily feasible using Roslyn ( .Net compiler service )
Take a look at eval()-like for C# using Roslyn and ScriptCS and Hosted Execution of smaller code snippets with Roslyn

Rsh
  • 7,214
  • 5
  • 36
  • 45