如何LINQPad编译代码? [英] How does LINQPad compile code?

查看:122
本文介绍了如何LINQPad编译代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我猜测它不会激活CSC.EXE或实现一个完整的编译器,所以它是如何工作的。



更​​新:感谢乔恩斯基特为指针代码这是容易学习。

 串c = @
公共A级
{
公共静态无效的主要(字串[] args)
{
的System.Console.WriteLine(世界你好);
}
}
;

CodeDomProvider编译器=新CSharpCodeProvider();

CompilerParameters参数=新CompilerParameters();
parameters.WarningLevel = 4;
parameters.GenerateExecutable = FALSE;
parameters.GenerateInMemory = TRUE;

CompilerResults R = compiler.CompileAssemblyFromSource(参数,C);

大会提出= r.CompiledAssembly;

类型[] TS = a.GetTypes();

型T = TS [0];

对象o = t.GetMethod(主)调用(空,新的对象[] {新的String [] {}})。


解决方案

从的如何LINQPad作品




LINQPad编译使用.NET的CSharpCodeProvider查询(或VBCodeProvider)




显然还有相当多吧,但是这是你问起位 - 阅读链接查看更多细节<。 / p>

如果你想看看一个较为简单的实现,你可以下载源代码了解Snippy,小工具,我对C#的深度创建。此外,它采用 CSharpCodeProvider - 这是一个很简单的例子,这很容易理解,如果运气好的话。 (这里涉及只有少数几类,IIRC)。


I am guessing it neither invokes csc.exe or implement an entire compiler, so how does it work?

Update: Thanks to Jon Skeet for the pointer to code that was easy to learn from.

string c = @"
public class A
{
    public static void Main(string[] args)
    {
        System.Console.WriteLine(""hello world"");
    }
}
";

CodeDomProvider compiler = new CSharpCodeProvider();

CompilerParameters parameters = new CompilerParameters();
parameters.WarningLevel = 4;
parameters.GenerateExecutable = false;
parameters.GenerateInMemory = true;

CompilerResults r = compiler.CompileAssemblyFromSource(parameters, c);

Assembly a = r.CompiledAssembly;

Type[] ts = a.GetTypes();

Type t = ts[0];

object o = t.GetMethod("Main").Invoke(null, new object[] { new string[] { } });

解决方案

From "How LINQPad Works":

LINQPad compiles your queries using .NET's CSharpCodeProvider (or VBCodeProvider)

Obviously there's rather more to it, but that's the bit you asked about - read the link for more details.

If you want to have a look at a rather more simplistic implementation, you could download the source code for Snippy, the little tool I created for C# in Depth. Again, it uses CSharpCodeProvider - and it's a simple enough example that it's easy to understand, with any luck. (There are only a few classes involved, IIRC.)

这篇关于如何LINQPad编译代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆