CodeDomProvider.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // System.CodeDom.Compiler.CodeDomProvider.cs
  3. //
  4. // Author:
  5. // Daniel Stodden ([email protected])
  6. // Marek Safar ([email protected])
  7. // Gonzalo Paniagua Javier ([email protected])
  8. //
  9. // copyright (C) 2002,2003,2004,2005 Novell, Inc.
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System.ComponentModel;
  32. using System.Configuration;
  33. using System.IO;
  34. namespace System.CodeDom.Compiler
  35. {
  36. [ToolboxItem ("")]
  37. public abstract class CodeDomProvider : Component
  38. {
  39. //
  40. // Constructors
  41. //
  42. protected CodeDomProvider()
  43. {
  44. }
  45. //
  46. // Properties
  47. //
  48. public virtual string FileExtension {
  49. get {
  50. return String.Empty;
  51. }
  52. }
  53. public virtual LanguageOptions LanguageOptions {
  54. get {
  55. return LanguageOptions.None;
  56. }
  57. }
  58. //
  59. // Methods
  60. //
  61. public abstract ICodeCompiler CreateCompiler();
  62. public abstract ICodeGenerator CreateGenerator();
  63. public virtual ICodeGenerator CreateGenerator (string fileName)
  64. {
  65. return CreateGenerator();
  66. }
  67. public virtual ICodeGenerator CreateGenerator (TextWriter output)
  68. {
  69. return CreateGenerator();
  70. }
  71. public virtual ICodeParser CreateParser()
  72. {
  73. return null;
  74. }
  75. public virtual TypeConverter GetConverter (Type type)
  76. {
  77. return TypeDescriptor.GetConverter (type);
  78. }
  79. #if NET_2_0
  80. public virtual CompilerResults CompileAssemblyFromDom (CompilerParameters options, params CodeCompileUnit[] compilationUnits)
  81. {
  82. return CreateCompiler ().CompileAssemblyFromDomBatch (options, compilationUnits);
  83. }
  84. public static CodeDomProvider CreateProvider (string language)
  85. {
  86. return GetCompilerInfo (language).CreateProvider ();
  87. }
  88. public virtual void GenerateCodeFromCompileUnit (CodeCompileUnit compileUnit,
  89. TextWriter writer, CodeGeneratorOptions options)
  90. {
  91. CreateGenerator ().GenerateCodeFromCompileUnit (compileUnit, writer, options);
  92. }
  93. public virtual void GenerateCodeFromStatement (CodeStatement statement, TextWriter writer, CodeGeneratorOptions options)
  94. {
  95. CreateGenerator ().GenerateCodeFromStatement (statement, writer, options);
  96. }
  97. public static CompilerInfo GetCompilerInfo (string language)
  98. {
  99. if (language == null)
  100. throw new ArgumentNullException ("language");
  101. return Config.GetCompilerInfo (language);
  102. }
  103. public static bool IsDefinedExtension (string extension)
  104. {
  105. if (extension == null)
  106. throw new ArgumentNullException ("extension");
  107. foreach (CompilerInfo c in Config.Compilers.Hash.Values) {
  108. if (Array.IndexOf (c.GetExtensions (), extension) != -1)
  109. return true;
  110. }
  111. return false;
  112. }
  113. public static bool IsDefinedLanguage (string language)
  114. {
  115. if (language == null)
  116. throw new ArgumentNullException ("language");
  117. return (Config.GetCompilerInfo (language) == null);
  118. }
  119. public virtual bool Supports (GeneratorSupport supports)
  120. {
  121. return CreateGenerator ().Supports (supports);
  122. }
  123. static CompilationConfiguration Config {
  124. get { return ConfigurationSettings.GetConfig ("system.codedom") as CompilationConfiguration; }
  125. }
  126. #endif
  127. }
  128. }