2010-04-18 3 views
2

MSDN에서 자습서를 사용하여 약간의 C# 컴파일러를 직접 만들었으므로 제대로 작동하지 않습니다. 몇 가지 오류가 발생하면 문제를 해결 한 다음 새로운 오류를 얻은 다음 문제를 해결합니다.C# 및 메타 데이터 파일 오류

최근 오류는 정말 혼란 스럽습니다.

--------------------------- 

--------------------------- 
Line number: 0, Error number: CS0006, 'Metadata file 'System.Linq.dll' could not be found; 




--------------------------- 
OK 
--------------------------- 

나는 이것이 무엇을 의미하는지 모른다.

누군가 여기서 무슨 일이 일어 났는지 설명해 주실 수 있습니까?

여기 내 코드입니다.

내 샘플 C# 컴파일러 코드 : using System; 당신이 볼 수 있듯이, 모든 사용 지침이있다,

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      string[] f = { "Form1.cs", "Form1.Designer.cs", "Program.cs" }; 
      string[] ra = { "System.dll", "System.Windows.Forms.dll", "System.Data.dll", "System.Drawing.dll", "System.Deployment.dll", "System.Xml.dll", "System.Linq.dll" }; 
      JTS.CSCompiler CSC = new JTS.CSCompiler(); 
      MessageBox.Show(CSC.Compile(
       textBox1.Text, @"Test Application.exe", ra, f, false)); 
     } 
    } 
} 

그래서 :

namespace JTM 
{ 
    public class CSCompiler 
    { 
     protected string ot, 
      rt, 
      ss, es; 

     protected bool rg, cg; 

     public string Compile(String se, String fe, String[] rdas, String[] fs, Boolean rn) 
     { 
      System.CodeDom.Compiler.CodeDomProvider CODEPROV = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("CSharp"); 
      ot = 
       fe; 

      System.CodeDom.Compiler.CompilerParameters PARAMS = new System.CodeDom.Compiler.CompilerParameters(); 
      // Ensure the compiler generates an EXE file, not a DLL. 
      PARAMS.GenerateExecutable = true; 
      PARAMS.OutputAssembly = ot; 

      foreach (String ay in rdas) 
      { 
       if (ay.Contains(".dll")) 
        PARAMS.ReferencedAssemblies.Add(ay); 
       else 
       { 
        string refd = ay; 
        refd = refd + ".dll"; 
        PARAMS.ReferencedAssemblies.Add(refd); 
       } 

      } 

      System.CodeDom.Compiler.CompilerResults rs = CODEPROV.CompileAssemblyFromFile(PARAMS, fs); 

      if (rs.Errors.Count > 0) 
      { 
       foreach (System.CodeDom.Compiler.CompilerError COMERR in rs.Errors) 
       { 
        es = es + 
         "Line number: " + COMERR.Line + 
         ", Error number: " + COMERR.ErrorNumber + 
         ", '" + COMERR.ErrorText + ";" + 
         Environment.NewLine + Environment.NewLine; 
       } 
      } 
      else 
      { 
       // Compilation succeeded. 
       es = "Compilation Succeeded."; 

       if (rn) System.Diagnostics.Process.Start(ot); 
      } 
      return es; 
     } 
    } 
} 

... 그리고 여기에 위의 클래스에 코드를 전달하는 응용 프로그램입니다. 나는이 오류의 의미를 모른다. 어떤 도움이라도 대단히 감사합니다.

이 추가 :

+0

난 당신이 있지만, 함께 무슨 말을하는지 이해 나, 그것이 비밀스럽지 않거나, 그것이 나를 생각하게하지 않는다면, 나는 결코 그것을 기억하지 못할 것이다. 그리고, 그들은 정말로 의미가 없습니다 : rdas는 "Referenced Assemblies"를 의미하고, se는 "Source"를 의미합니다. :) –

+2

@jts : 몇 달 후에 코드를 방문하면 "refrdAsmbls", "src"등등과 같은 이름을 사용하여 자신을 저주합니다. –

+0

기억하기 : 코딩 스타일이 나와 다른 누구에게도 적합하지 않다면 , 다른 사람들이 당신을 도우려는 것이 더 어려워집니다. – Joel

답변

9

솔루션 감사

PARAMS.ReferencedAssemblies.Add(typeof(System.Xml.Linq.Extensions).Assembly.Location); 

그래서 코드는 이제 다음과 같습니다

namespace JTM 
{ 
    public class CSCompiler 
    { 
     protected string ot, 
      rt, 
      ss, es; 

     protected bool rg, cg; 

     public string Compile(String se, String fe, String[] rdas, String[] fs, Boolean rn) 
     { 
      System.CodeDom.Compiler.CodeDomProvider CODEPROV = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("CSharp"); 
      ot = 
       fe; 

      System.CodeDom.Compiler.CompilerParameters PARAMS = new System.CodeDom.Compiler.CompilerParameters(); 
      // Ensure the compiler generates an EXE file, not a DLL. 
      PARAMS.GenerateExecutable = true; 
      PARAMS.OutputAssembly = ot; 
      PARAMS.ReferencedAssemblies.Add(typeof(System.Xml.Linq.Extensions).Assembly.Location); 
      foreach (String ay in rdas) 
      { 
       if (ay.Contains(".dll")) 
        PARAMS.ReferencedAssemblies.Add(ay); 
       else 
       { 
        string refd = ay; 
        refd = refd + ".dll"; 
        PARAMS.ReferencedAssemblies.Add(refd); 
       } 

      } 

      System.CodeDom.Compiler.CompilerResults rs = CODEPROV.CompileAssemblyFromFile(PARAMS, fs); 

      if (rs.Errors.Count > 0) 
      { 
       foreach (System.CodeDom.Compiler.CompilerError COMERR in rs.Errors) 
       { 
        es = es + 
         "Line number: " + COMERR.Line + 
         ", Error number: " + COMERR.ErrorNumber + 
         ", '" + COMERR.ErrorText + ";" + 
         Environment.NewLine + Environment.NewLine; 
       } 
      } 
      else 
      { 
       // Compilation succeeded. 
       es = "Compilation Succeeded."; 

       if (rn) System.Diagnostics.Process.Start(ot); 
      } 
      return es; 
     } 
    } 
} 
+0

니스. 방금도이 문제를 겪었습니다. 상대 경로 나 절대 경로없이 .dll 이름 만 사용했습니다. 나는 컴파일러가 어셈블리의 "현재"디렉토리를 조사했을 것이라고 생각했다. –

관련 문제