I just have a very simple question, does C# have command like this? DoCommand(“int x = 20”); or just like Eval () in JavaScript?
I believe Roslyn can help with this. See this answer on stack:
https://stackoverflow.com/a/13321964

This is a good answer, but I’d like to add that it’s for advanced programmers e.g. compiler authors. Newer programmers should learn how to restructure their code to not need it.

There’s more complicated reflection and code generation stuff, but nothing as simple as a straight up “compile and run this string”
A better question to ask is why you’re trying to do that.

I created a C# expression evaluator and used it across many projects that needed a custom data binding solution, somewhere a user defined expression needs to be evaluated against runtime values. Report Templating for example.

I have used DynamicExpresso for that, works great and is easy to use.
I built an expression evaluator. It’s on Nuget.
https://www.nuget.org/packages/ExpressionEvaluator/

It was bought by another company with a competing product, so it now says “no longer supported”. But it works for most cases.
There is also dynamicexpresso:
https://github.com/davideicardi/DynamicExpresso
And of course the Roslyn Scripting API.

The things you want to look for is a way to set the “context” of an expression. A lot of times you want the code to act on variables during runtime.

These compilera generally create a Func<> or Action<>. They take your text code and compile it into a C# delegate. As a delegate or Func or Action, it can be assigned to a variable, and most impotantly, cached.

Compilation is fast for what are usually small snippets of code, but if you do it in a loop repeated compilation of the same code is wasteful, so I usually cache it into a Dictionary.

As far as I am aware, no, though I suppose the closest to it would be reflection. Either way it seems like using eval is just inciting danger
Not accurate, this can be done multiple ways. Im sure there are FOSS libraries out there to help with some of these more difficult APIs
Roslyn – Compiler as a service
Expression Trees
IL Emitter

Also this seems to me like json parsing is the underlying goal. I’m more curious why, cause the code smell and simplicity of the question alludes he is going down the wrong path.

No, there’s no Eval function, since c# needs to be compiled to intermediate language to be executed.
That being said, you can use CodeDom to compile and run a piece of c#, at runtime. See for example: https://stackoverflow.com/questions/826398/is-it-possible-to-dynamically-compile-and-execute-c-sharp-code-fragments
Expression trees can do something similar (execute arbitrary expressions at runtime.)
Datatable.Compute
https://github.com/dotnet/interactive/
Yes it does.
If you are using it then you are probably seriously fucking up.

source