質問

Dom からアセンブリをコンパイルする よりも速い ソースからアセンブリをコンパイル?

それ すべき おそらくコンパイラのフロントエンドをバイパスするためです。

役に立ちましたか?

解決

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 and Settings\jacob\Local Settings emp\x59n9yb-.0.cs:17

;予想される - c:\Documents and Settings\jacob\Local Settings emp\x59n9yb-.0.cs:17

無効な式用語 ')' - c:\Documents and Settings\jacob\Local Settings emp\x59n9yb-.0.cs:17

したがって、答えは「ノー」だと思います

他のヒント

以前に究極のコンパイラ呼び出しを見つけようとしましたが、諦めました。私の忍耐力のために、インターフェイスと仮想クラスのレイヤーがかなりの数あります。

コンパイラのソース リーダー部分が最終的に DOM ツリーになるとは思いませんが、直感的にはあなたの意見に同意します。DOM を IL に変換するために必要な作業は、C# ソース コードを読み取るよりもはるかに少なくなります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top