2016-07-21 4 views
0

私はcodedamサンプルプロジェクトを正常に実行しました。 出力ファイルはbinデバッグフォルダーにあります。CodeDomは生成された.csファイルをどこに保存しますか?この場所を別のフォルダに変更することができます

C#クラスファイルを生成中です。 D:/ Tempフォルダに生成するC#クラスファイルが必要 実行ファイルが生成されていません 出力はC#.csファイルです。

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.CodeDom.Compiler; 
using System.CodeDom; 
using Microsoft.CSharp; 
using System.Reflection; 


namespace codeDamnSample 
{ 


    public class CCodeGenerator 
    { 
     CodeNamespace mynamespace; 
     CodeTypeDeclaration myclass; 
     CodeCompileUnit myassembly; 

     public void CreateNamespace() 
     { 
      mynamespace = new CodeNamespace("mynamespace"); 
     } 
     public void CreateImports() 
     { 
      mynamespace.Imports.Add(new CodeNamespaceImport("System")); 

     } 
     public void CreateClass() 
     { 
      myclass = new CodeTypeDeclaration(); 
      myclass.Name = "SampleTest:TestBase"; 
      myclass.IsClass = true; 
      myclass.Attributes = MemberAttributes.Public; 
      mynamespace.Types.Add(myclass); 
     } 
     public void CreateMethod() 
     { 
      CodeMemberMethod mymethod = new CodeMemberMethod(); 
      mymethod.Name = testMethod; 
      CodeTypeReference ctr = new CodeTypeReference(); 
      //Assign the return type to the method. 
      mymethod.ReturnType = ctr; 
      //Provide definition to the method (returns the sum of two //numbers) 
      CodeSnippetExpression snippet1 = new CodeSnippetExpression("AutomationBase obj = new AutomationBase()"); 
      //return the value 
      CodeSnippetExpression snippet2 = new CodeSnippetExpression("obj.Execute(testCases[0])"); 
      //Convert the snippets into Expression statements. 
      CodeExpressionStatement stmt1 = new CodeExpressionStatement(snippet1); 
      CodeExpressionStatement stmt2 = new CodeExpressionStatement(snippet2); 
      //Add the expression statements to the method. 
      mymethod.Statements.Add(stmt1); 
      mymethod.Statements.Add(stmt2); 
      //Provide the access modifier for the method. 
      mymethod.Attributes = MemberAttributes.Public; 
      CodeAttributeDeclaration codeAttrDecl = new CodeAttributeDeclaration("Test"); 

      mymethod.CustomAttributes.Add(codeAttrDecl); 
      //Finally add the method to the class. 
      myclass.Members.Add(mymethod); 
     } 
     public void SaveAssembly() 
     { 
      myassembly = new CodeCompileUnit(); 
      myassembly.Namespaces.Add(mynamespace); 
      CompilerParameters comparam = new CompilerParameters(new string[] { "mscorlib.dll" }); 
      comparam.GenerateInMemory = false; 
      comparam.GenerateExecutable = false; 
      comparam.MainClass = "mynamespace.CMyclass"; 
      Microsoft.CSharp.CSharpCodeProvider ccp = new Microsoft.CSharp.CSharpCodeProvider(); 

      String sourceFile; 
      ccp. 
      if (ccp.FileExtension[0] == '.') 
      { 
       sourceFile = "SampleTest" + ccp.FileExtension; 
      } 
      else 
      { 
       sourceFile = "SampleTest." + ccp.FileExtension; 
      } 
      var tw1 = new IndentedTextWriter(new StreamWriter(sourceFile, false), " "); 
      ccp.GenerateCodeFromCompileUnit(myassembly, tw1, new CodeGeneratorOptions()); 
      tw1.Close(); 

      ccp.CompileAssemblyFromDom(comparam, myassembly); 


     } 
     public static void generateCod() 
     { 
      CCodeGenerator cg = new CCodeGenerator(); 
      cg.CreateNamespace(); 
      cg.CreateImports(); 
      cg.CreateClass(); 
      cg.CreateMember(); 
      cg.CreateProperty(); 
      cg.CreateMethod(); 

      // cg.CreateEntryPoint(); 
      cg.SaveAssembly(); 
      // Console.ReadLine(); 
     } 


    } 


} 

答えて

0

それはあなたの実行中のプロジェクトでは、通常bin/Debugフォルダ、現在の作業ディレクトリにファイルを保存する必要があります。

+0

他のフォルダに場所を指定することができます – Lijo

+0

あなたのプロジェクト設定で。しかし、あなたはむしろ 'cp.OutputAssembly'のパスを変更するでしょう。 –

+0

あなたは精巧にできますか?理解できません – Lijo

関連する問題