C# CSharpCodeProvider 编译性能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CSharpCodeProvider Compilation Performance
提问by Andrew Peters
Is CompileAssemblyFromDomfaster than CompileAssemblyFromSource?
是CompileAssemblyFromDom速度比CompileAssemblyFromSource?
It shouldbe as it presumably bypasses the compiler front-end.
它应该是因为它可能绕过编译器前端。
采纳答案by Jacob Krall
CompileAssemblyFromDom compiles to a .cs file which is then run through the normal C# compiler.
CompileAssemblyFromDom 编译为 .cs 文件,然后通过普通的 C# 编译器运行该文件。
Example:
例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CSharp;
using System.CodeDom;
using System.IO;
using System.CodeDom.Compiler;
using System.Reflection;
namespace CodeDomQuestion
{
class Program
{
private static void Main(string[] args)
{
Program p = new Program();
p.dotest("C:\fs.exe");
}
public void dotest(string outputname)
{
CSharpCodeProvider cscProvider = new CSharpCodeProvider();
CompilerParameters cp = new CompilerParameters();
cp.MainClass = null;
cp.GenerateExecutable = true;
cp.OutputAssembly = outputname;
CodeNamespace ns = new CodeNamespace("StackOverflowd");
CodeTypeDeclaration type = new CodeTypeDeclaration();
type.IsClass = true;
type.Name = "MainClass";
type.TypeAttributes = TypeAttributes.Public;
ns.Types.Add(type);
CodeMemberMethod cmm = new CodeMemberMethod();
cmm.Attributes = MemberAttributes.Static;
cmm.Name = "Main";
cmm.Statements.Add(new CodeSnippetExpression("System.Console.WriteLine('f'zxcvv)"));
type.Members.Add(cmm);
CodeCompileUnit ccu = new CodeCompileUnit();
ccu.Namespaces.Add(ns);
CompilerResults results = cscProvider.CompileAssemblyFromDom(cp, ccu);
foreach (CompilerError err in results.Errors)
Console.WriteLine(err.ErrorText + " - " + err.FileName + ":" + err.Line);
Console.WriteLine();
}
}
}
which shows errors in a (now nonexistent) temp file:
它显示了(现在不存在的)临时文件中的错误:
) expected - c:\Documents and Settings\jacob\Local Settings\Temp\x59n9yb-.0.cs:17
; expected - c:\Documents and Settings\jacob\Local Settings\Temp\x59n9yb-.0.cs:17
Invalid expression term ')' - c:\Documents and Settings\jacob\Local Settings\Tem p\x59n9yb-.0.cs:17
) 预期 - c:\Documents and Settings\jacob\Local Settings\Temp\x59n9yb-.0.cs:17
; 预期 - c:\Documents and Settings\jacob\Local Settings\Temp\x59n9yb-.0.cs:17
无效的表达式术语 ')' - c:\Documents and Settings\jacob\Local Settings\Tem p\x59n9yb-.0.cs:17
So I guess the answer is "no"
所以我想答案是“不”
回答by Lasse V. Karlsen
I've tried finding the ultimate compiler call earlier and I gave up. There's quite a number of layers of interfaces and virtual classes for my patience.
我之前尝试过找到最终的编译器调用,但我放弃了。有相当多的接口层和虚拟类让我耐心等待。
I don't think the source reader part of the compiler ends up with a DOM tree, but intuitively I would agree with you. The work necessary to transform the DOM to IL should be much less than reading C# source code.
我不认为编译器的源代码阅读器部分会以 DOM 树结束,但从直觉上我会同意你的看法。将 DOM 转换为 IL 所需的工作应该比阅读 C# 源代码少得多。