CSCompiler.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.Text;
  15. using System.Reflection;
  16. using Microsoft.CSharp;
  17. namespace System.Web.Compilation
  18. {
  19. class CSCompiler
  20. {
  21. static CodeDomProvider provider;
  22. static ICodeCompiler compiler;
  23. string filename;
  24. ArrayList assemblies;
  25. CompilerParameters options;
  26. static CSCompiler ()
  27. {
  28. provider = new CSharpCodeProvider ();
  29. compiler = provider.CreateCompiler ();
  30. }
  31. private CSCompiler (string filename, ArrayList assemblies)
  32. {
  33. this.filename = filename;
  34. this.assemblies = assemblies;
  35. options = new CompilerParameters ();
  36. if (assemblies != null) {
  37. StringCollection coll = new StringCollection ();
  38. foreach (string str in assemblies)
  39. coll.Add (str);
  40. }
  41. }
  42. public Assembly GetCompiledAssembly ()
  43. {
  44. CompilerResults results = compiler.CompileAssemblyFromFile (options, filename);
  45. if (results.NativeCompilerReturnValue != 0) {
  46. StringBuilder errors = new StringBuilder ();
  47. foreach (CompilerError error in results.Errors) {
  48. errors.Append (error);
  49. errors.Append ('\n');
  50. }
  51. throw new HttpException ("Error compiling " + filename + "\n" + errors.ToString ());
  52. }
  53. return results.CompiledAssembly;
  54. }
  55. static public Assembly CompileCSFile (string file, ArrayList assemblies)
  56. {
  57. CSCompiler compiler = new CSCompiler (file, assemblies);
  58. return compiler.GetCompiledAssembly ();
  59. }
  60. static public CodeDomProvider Provider {
  61. get { return provider; }
  62. }
  63. static public ICodeCompiler Compiler {
  64. get { return compiler; }
  65. }
  66. }
  67. }