人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當前位置:蘿卜系統下載站 > 技術開發教程 > 詳細頁面

運用CodeDom來生成.cs文件

運用CodeDom來生成.cs文件

更新時間:2022-10-03 文章作者:未知 信息來源:網絡 閱讀次數:

在學使用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 編程->動態生成和編譯以多種語言表示的源代碼

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統下載排行

網站地圖xml | 網站地圖html
主站蜘蛛池模板: 明光市| 乌兰县| 柏乡县| 沙雅县| 定边县| 南召县| 苍南县| 长治市| 分宜县| 建湖县| 霍城县| 清新县| 西城区| 公主岭市| 枝江市| 施甸县| 盘锦市| 岚皋县| 梧州市| 广丰县| 河东区| 吉木乃县| 汝阳县| 兴城市| 北碚区| 古丈县| 麻栗坡县| 太仓市| 嘉峪关市| 乡城县| 毕节市| 阳原县| 历史| 万宁市| 偃师市| 方城县| 抚宁县| 石门县| 千阳县| 无锡市| 探索|