zoukankan      html  css  js  c++  java
  • 使用CodeDom来生成.cs文件

    在学使用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 编程->动态生成和编译以多种语言表示的源代码

  • 相关阅读:
    加载时间分析与优化
    t
    linux 3389
    切片声明 切片在内存中的组织方式 reslice
    从模版生成 uri Golang 的 html/template 包不太适合于这种情况
    负载均衡实现,一个域名对应多个IP地址
    京东首页 淘宝首页 图片加载 单域名 多域名 图片服务
    Reduce DNS Lookups 减少DNS查找
    Make Fewer HTTP Requests 减少HTTP请求
    What the 80/20 Rule Tells Us about Reducing HTTP Requests
  • 原文地址:https://www.cnblogs.com/dahuzizyd/p/18119.html
Copyright © 2011-2022 走看看