2
0

CSCompiler.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //
  2. // System.Web.Compilation.CSCompiler
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2003 Ximian, Inc (http://www.ximian.com)
  8. //
  9. using System;
  10. using System.CodeDom;
  11. using System.CodeDom.Compiler;
  12. using System.Collections;
  13. using System.Collections.Specialized;
  14. using System.IO;
  15. using System.Text;
  16. using System.Reflection;
  17. using Microsoft.CSharp;
  18. namespace System.Web.Compilation
  19. {
  20. class CSCompiler
  21. {
  22. static CodeDomProvider provider;
  23. static ICodeCompiler compiler;
  24. string filename;
  25. ArrayList assemblies;
  26. CompilerParameters options;
  27. static CSCompiler ()
  28. {
  29. provider = new CSharpCodeProvider ();
  30. compiler = provider.CreateCompiler ();
  31. }
  32. private CSCompiler (string filename, ArrayList assemblies)
  33. {
  34. this.filename = filename;
  35. this.assemblies = assemblies;
  36. options = new CompilerParameters ();
  37. if (assemblies != null) {
  38. StringCollection coll = options.ReferencedAssemblies;
  39. foreach (string str in assemblies)
  40. coll.Add (str);
  41. }
  42. }
  43. public Assembly GetCompiledAssembly ()
  44. {
  45. CompilerResults results = compiler.CompileAssemblyFromFile (options, filename);
  46. if (results.NativeCompilerReturnValue != 0) {
  47. StreamReader reader = new StreamReader (filename);
  48. throw new CompilationException (filename, results.Errors, reader.ReadToEnd ());
  49. }
  50. return results.CompiledAssembly;
  51. }
  52. static public Assembly CompileCSFile (string file, ArrayList assemblies)
  53. {
  54. CSCompiler compiler = new CSCompiler (file, assemblies);
  55. return compiler.GetCompiledAssembly ();
  56. }
  57. static public CodeDomProvider Provider {
  58. get { return provider; }
  59. }
  60. static public ICodeCompiler Compiler {
  61. get { return compiler; }
  62. }
  63. }
  64. }