2016-07-21 5 views
0

Ich habe das Codedam-Beispielprojekt erfolgreich ausgeführt. Die Out-Put-Datei befindet sich im bin Debug-Ordner.Wo speichert CodeDom generierte CS-Dateien? Können wir diesen Ort in einen anderen Ordner ändern?

Ich erzeuge eine C# -Klasse-Datei. Ich brauche diese C# -Klassendatei in generiert werden D:/Temp-Ordner bin keine ausführbare output generieren ist C# .cs-Datei.

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(); 
     } 


    } 


} 

Antwort

0

Es sollte die Datei im aktuellen Arbeitsverzeichnis speichern, in der Regel die bin/Debug Ordner in Ihrem laufenden Projekt.

+0

können wir den Speicherort zu einem anderen Ordner – Lijo

+0

In Ihren Projekteinstellungen angeben. Aber Sie würden lieber den Pfad in 'cp.OutputAssembly' ändern. –

+0

Können Sie es ausarbeiten..ich kann das nicht verstehen – Lijo

Verwandte Themen