简体   繁体   English

如何使用C#代码运行C#代码?

[英]How to run C# code using C# code?

i had asked earlier about the same kind of question but that was in java.... 我早些时候曾问过同样的问题,但那是在java ....
now for the knowledge, i want to know... 现在为了知识,我想知道......

is it possible to run the one C# code using another C# code? 是否可以使用另一个C#代码运行一个C#代码?

bcz i know C# is very powerful, so their might be some way to do this. bcz我知道C#非常强大,所以他们可能会有一些方法来做到这一点。

It's the same sort of deal: 这是同样的交易:

  • Compile the code, eg using CSharpCodeProvider 编译代码,例如使用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. 如果你从深度网站的C# 下载Snippy ,你可以看到一个小例子 - 基本上你输入C#的片段,它可以为你编译和执行它们。

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. 在MS .NET中, CSharpCodeProvider是您目前最接近的,尽管他们已经提到将来可能会将“编译器作为服务”。

If you're happy to use Mono, it already exists , with REPL example . 如果您乐意使用Mono, 它已经存在 ,使用REPL示例

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. 这有CodeDom命名空间。 Check this article 看看这篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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