C#高级应用之CodeDomProvider类
来源:互联网 发布:淘宝客服怎么认证 编辑:程序博客网 时间:2024/05/20 07:50
https://msdn.microsoft.com/en-us/library/microsoft.csharp.csharpcodeprovider.aspx
using System;using System.IO;using System.Globalization;using System.CodeDom.Compiler;using System.Text;using Microsoft.CSharp;using Microsoft.VisualBasic;namespace CodeProviders{class CompileSample{ [STAThread] static void Main(string[] args) { if (args.Length > 0) { // First parameter is the source file name. if (File.Exists(args[0])) { CompileExecutable(args[0]); } else { Console.WriteLine("Input source file not found - {0}", args[0]); } } else { Console.WriteLine("Input source file not specified on command line!"); } } public static bool CompileExecutable(String sourceName) { FileInfo sourceFile = new FileInfo(sourceName); CodeDomProvider provider = null; bool compileOk = false; // Select the code provider based on the input file extension. if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".CS") { provider = CodeDomProvider.CreateProvider("CSharp"); } else if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".VB") { provider = CodeDomProvider.CreateProvider("VisualBasic"); } else { Console.WriteLine("Source file must have a .cs or .vb extension"); } if (provider != null) { // Format the executable file name. // Build the output assembly path using the current directory // and <source>_cs.exe or <source>_vb.exe. String exeName = String.Format(@"{0}\{1}.exe", System.Environment.CurrentDirectory, sourceFile.Name.Replace(".", "_")); CompilerParameters cp = new CompilerParameters(); // Generate an executable instead of // a class library. cp.GenerateExecutable = true; // Specify the assembly file name to generate. cp.OutputAssembly = exeName; // Save the assembly as a physical file. cp.GenerateInMemory = false; // Set whether to treat all warnings as errors. cp.TreatWarningsAsErrors = false; // Invoke compilation of the source file. CompilerResults cr = provider.CompileAssemblyFromFile(cp, sourceName); if(cr.Errors.Count > 0) { // Display compilation errors. Console.WriteLine("Errors building {0} into {1}", sourceName, cr.PathToAssembly); foreach(CompilerError ce in cr.Errors) { Console.WriteLine(" {0}", ce.ToString()); Console.WriteLine(); } } else { // Display a successful compilation message. Console.WriteLine("Source {0} built into {1} successfully.", sourceName, cr.PathToAssembly); } // Return the results of the compilation. if (cr.Errors.Count > 0) { compileOk = false; } else { compileOk = true; } } return compileOk; }}}
阅读全文
0 0
- C#高级应用之CodeDomProvider类
- C#高级应用之CodeDomProvider引擎篇
- C#高级应用之CodeDomProvider引擎篇 .
- C#枚举高级应用
- C#中CodeDomProvider代码编译的简单实现
- C#高级应用之Microsoft.Vsa引擎篇
- C#图片裁剪高级应用
- C# 高级应用--反射动态调用类的成员范例 !
- c#事务、using释放资源、实体类的高级应用
- C#高级编程之“反射”
- C#高级编程之“反射”
- c#高级编程之常量
- java基础之高级应用
- C宏高级应用之##/#
- JSP高级应用之“过滤器“
- JSP高级应用之“过滤器“
- JSP高级应用之“监听器“
- Redis学习之高级应用
- 芯片——摩尔定律的传奇(下)
- webservice接口编写
- Django中REST风格数据传输
- web安全之CSRF
- fatal: refusing to merge unrelated histories
- C#高级应用之CodeDomProvider类
- 文档格式转换技巧全记录,超完整的文档格式转换方式
- Fragment+webview实现物理返回键的监听
- 手机号、银行卡号格式化
- string首尾去空格
- integer源码mark
- crontab的格式
- MySQL优化(1)
- 欢迎使用CSDN-markdown编辑器