CompileAssemblyFromDom比CompileAssemblyFromSource快吗?
这应该是因为它应该绕过编译器前端。
发布于 2008-08-27 01:03:17
CompileAssemblyFromDom编译成一个.cs文件,然后通过普通的C#编译器运行。
示例:
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();
}
}
}
它显示了(现在不存在)临时文件中的错误:
(
)预期- c:\Documents和Settings\jacob\Local \Temp\x59n9yb-.0.cs:17
;预期- c:\Documents和Settings\jacob\Local \Temp\x59n9yb-.0.cs:17
无效表达式术语‘’- c:\Documents和Settings\jacob\Local \Tem p\x59n9yb-.0.cs:17
所以我想答案是“不”
发布于 2008-08-07 12:48:29
我之前尝试过找到最终的编译器调用,但我放弃了。对于我的耐心,有很多层的接口和虚拟类。
我不认为编译器的源代码读取器以DOM树结束,但直觉上我会同意您的意见。将DOM转换为IL所需的工作应该比读取C#源代码少得多。
https://stackoverflow.com/questions/4612
复制相似问题