首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >CSharpCodeProvider编译性能

CSharpCodeProvider编译性能
EN

Stack Overflow用户
提问于 2008-08-07 12:01:14
回答 2查看 3.2K关注 0票数 20

CompileAssemblyFromDom比CompileAssemblyFromSource快吗?

这应该是因为它应该绕过编译器前端。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2008-08-27 01:03:17

CompileAssemblyFromDom编译成一个.cs文件,然后通过普通的C#编译器运行。

示例:

代码语言:javascript
运行
复制
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

所以我想答案是“不”

票数 9
EN

Stack Overflow用户

发布于 2008-08-07 12:48:29

我之前尝试过找到最终的编译器调用,但我放弃了。对于我的耐心,有很多层的接口和虚拟类。

我不认为编译器的源代码读取器以DOM树结束,但直觉上我会同意您的意见。将DOM转换为IL所需的工作应该比读取C#源代码少得多。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4612

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档