2011-11-25 49 views
3

我有一個CSharpCodeProvider它需要一個類的代碼。在正在編譯代碼的項目中,我有一個接口。我想要編譯的代碼符合這個接口。CSharpCodeProvider符合接口

下面是我能想到的最簡單的例子來說明我的問題。我有兩個文件的項目:

的Program.cs:

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      //set up the compiler 
      CSharpCodeProvider csCompiler = new CSharpCodeProvider(); 

      CompilerParameters compilerParameters = new CompilerParameters(); 
      compilerParameters.GenerateInMemory = true; 
      compilerParameters.GenerateExecutable = false; 

      var definition = 
@"class Dog : IDog 
{ 
    public void Bark() 
    { 
     //woof 
    } 
}";  
      CompilerResults results = csCompiler.CompileAssemblyFromSource(compilerParameters, new string[1] { definition }); 

      IDog dog = null; 
      if (results.Errors.Count == 0) 
      { 
       Assembly assembly = results.CompiledAssembly; 
       dog = assembly.CreateInstance("TwoDimensionalCellularAutomatonDelegate") as IDog; 
      } 

      if (dog == null) 
       dog.Bark(); 
     } 
    } 
} 

IDog.cs:

namespace ConsoleApplication1 
{ 
    interface IDog 
    { 
     void Bark(); 
    } 
} 

我似乎無法弄清楚如何獲得CSharpCodeProvider承認IDog 。我試過compilerParameters.ReferencedAssemblies.Add("ConsoleApplication1");,但它沒有奏效。任何幫助,將不勝感激。

+0

我想你需要在編譯時有相同的程序集。換句話說,'csc /r:IDoc.dll dog.cs'是不合格的。 'csc dog.cs IDoc.cs'確定。 – BLUEPIXY

+0

我並不完全遵循你所說的話。你可以進一步詳細一點嗎? – LandonSchropp

+0

請看我的答案。 – BLUEPIXY

回答

1

的解決方案是參考當前正在執行的組件。

var location = Assembly.GetExecutingAssembly().Location; 
compilerParameters.ReferencedAssemblies.Add(location); 
0

嘗試這種

using System; 
using System.IO; 
using System.CodeDom.Compiler; 
using System.Reflection; 
using Microsoft.CSharp; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      //set up the compiler 
      CSharpCodeProvider csCompiler = new CSharpCodeProvider(); 

      CompilerParameters compilerParameters = new CompilerParameters(); 
      compilerParameters.GenerateInMemory = true; 
      compilerParameters.GenerateExecutable = false; 
      compilerParameters.ReferencedAssemblies.Add("System.dll"); 

      var definition = File.ReadAllText("./IDog.cs") + 
@"public class Dog : ConsoleApplication1.IDog 
{ 
    public void Bark() 
    { 
     System.Console.WriteLine(""BowWoW""); 
    } 
}"; 
      CompilerResults results = csCompiler.CompileAssemblyFromSource(compilerParameters, 
       new string[1] { definition }); 

      dynamic dog = null; 
      if (results.Errors.Count == 0) 
      { 
       Assembly assembly = results.CompiledAssembly; 
//    Type idog = assembly.GetType("ConsoleApplication1.IDog"); 
       dog = assembly.CreateInstance("Dog"); 
      } else { 
       Console.WriteLine("Has Errors"); 
       foreach(CompilerError err in results.Errors){ 
        Console.WriteLine(err.ErrorText); 
       } 
      } 
      if (dog != null){ 
       dog.Bark(); 
      } else { 
       System.Console.WriteLine("null"); 
      } 
     } 
    } 
} 
+0

嗯,這似乎沒有工作。特別是,該文件找不到。看起來我必須將該文件與程序集一起存儲在此解決方案中。 – LandonSchropp

+0

@helixed一旦找不到任何文件,「IDog.cs」就是這樣。 – BLUEPIXY

0

優秀! 它可能是

IDog dog = (IDog)a.CreateInstance("Dog"); 
dog.Bark(/*additional parameters go here*/);