简体   繁体   中英

How to run C# code using C# code?

i had asked earlier about the same kind of question but that was in java....
now for the knowledge, i want to know...

is it possible to run the one C# code using another C# code?

bcz i know C# is very powerful, so their might be some way to do this.

It's the same sort of deal:

  • Compile the code, eg using CSharpCodeProvider
  • Execute the code, eg using reflection

If you download Snippy from the C# in Depth web site, you can see a smallish example of this - basically you type in snippets of C# and it can compile and execute them for you.

That is ultimately a runtime feature; in MS .NET, CSharpCodeProvider is the closest you'll get at the moment, although they have mentioned possibly looking at the "compiler as a service" in the future.

If you're happy to use Mono, it already exists , with REPL example .

Usage (from here ):

  // First setup the active using statements:
  Evaluator.Run ("using System;");

  Evaluator.Run ("Console.WriteLine (\"Hello, World\");

There is CodeDom namespace for this. Check this article

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM