CSharpCodeProvider 컴파일 성능
-
08-06-2019 - |
문제
~이다 컴파일AssemblyFromDom 보다 빠른 CompileAssemblyFromSource?
그것 ~해야 한다 아마도 컴파일러 프런트엔드를 우회하기 때문일 것입니다.
해결책
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 em p\x59n9yb-.0.cs:17
그래서 내 대답은 "아니요"인 것 같아요
다른 팁
이전에 궁극적인 컴파일러 호출을 찾으려고 노력했지만 포기했습니다.내 인내심을 위해 꽤 많은 인터페이스 계층과 가상 클래스가 있습니다.
나는 컴파일러의 소스 리더 부분이 DOM 트리로 끝난다고 생각하지 않지만 직관적으로 나는 당신의 의견에 동의합니다.DOM을 IL로 변환하는 데 필요한 작업은 C# 소스 코드를 읽는 것보다 훨씬 적습니다.
제휴하지 않습니다 StackOverflow