使用CodeDom來產生.cs檔案

來源:互聯網
上載者:User

在學使用CodeDom來動態產生.cs檔案,使用協助裡的例子,代碼居然編譯不通過
自己修改,調試通過,整理後主要代碼如下:

命名空間:
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.IO;

  private void button1_Click(object sender, System.EventArgs e)
  {
   CodeCompileUnit CompileUnit = new CodeCompileUnit();
   CodeNamespace Samples = new CodeNamespace("Samples");
   Samples.Imports.Add( new CodeNamespaceImport("System") );
   CompileUnit.Namespaces.Add( Samples );
   CodeTypeDeclaration Class1 = new CodeTypeDeclaration("Class1");
   Samples.Types.Add(Class1);

   CodeEntryPointMethod Start = new CodeEntryPointMethod();
   
   //輸出HelloWord
   CodeMethodInvokeExpression cs1 = new CodeMethodInvokeExpression( new
    CodeTypeReferenceExpression("System.Console"), "WriteLine", new
    CodePrimitiveExpression("Hello World!") );
   
   Start.Statements.Add(cs1);
   

   Class1.Members.Add( Start );
   //CSharpCodeProvider provider = new CSharpCodeProvider();
   //ICodeGenerator gen = provider.CreateGenerator();
   GenerateGraph(CompileUnit);

  }
  public void GenerateGraph(CodeCompileUnit compileunit)
  {
   // Obtains an ICodeGenerator from a CodeDomProvider class.
   CSharpCodeProvider provider = new CSharpCodeProvider();
   ICodeGenerator gen = provider.CreateGenerator();
  
   // Creates a StreamWriter to an output file.
   StreamWriter sw = new StreamWriter("d:\\TestGraph.cs", false);

   // Generates source code using the code generator.
   gen.GenerateCodeFromCompileUnit(compileunit, sw, new    CodeGeneratorOptions());
  
   // Closes the output files.
   sw.Close();
  }

  private void button2_Click(object sender, System.EventArgs e)
  {
   CompileCode("d:\\TestGraph.cs");
  }
  //編輯產生Exe
  public CompilerResults CompileCode(string filepath)
  {
   // Obtains an ICodeCompiler from a CodeDomProvider class.
   CSharpCodeProvider provider = new CSharpCodeProvider();
   ICodeCompiler compiler = provider.CreateCompiler();

   // Configures a compiler parameters object which links System.dll and
   // generates a file name based on the specified source file name.
   CompilerParameters cp = new CompilerParameters(new string[] {"System.dll"}, filepath.Substring(0, filepath.LastIndexOf(".")+1)+"exe", false);

   // Indicates that an executable rather than a .dll should be generated.
   cp.GenerateExecutable = true;

   // Invokes compilation.
   CompilerResults cr = compiler.CompileAssemblyFromFile(cp, filepath);  

   // Returns the results of compilation.
   return cr;       
  }

協助裡的例子在:
.NET Framework->使用 .NET Framework 編程->動態產生和編譯以多種語言表示的原始碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.