The Labs \ Source Viewer \ SSCLI \ System.CodeDom.Compiler \ CodeDomProvider

  1. //------------------------------------------------------------------------------
  2. // <copyright file="CodeDOMProvider.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. namespace System.CodeDom.Compiler
  16. {
  17.     using System;
  18.     using System.CodeDom;
  19.     using System.ComponentModel;
  20.     using System.IO;
  21.     using System.Security.Permissions;
  22.     using System.Configuration;
  23.     using System.Collections;
  24.     using System.Runtime.InteropServices;
  25.    
  26.     [ToolboxItem(false)]
  27.     [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
  28.     [PermissionSet(SecurityAction.InheritanceDemand, Name = "FullTrust")]
  29.     [ComVisible(true)]
  30.     public abstract class CodeDomProvider : Component
  31.     {
  32.        
  33.         [ComVisible(false)]
  34.         public static CodeDomProvider CreateProvider(string language)
  35.         {
  36.             CompilerInfo compilerInfo = GetCompilerInfo(language);
  37.             return compilerInfo.CreateProvider();
  38.         }
  39.        
  40.         [ComVisible(false)]
  41.         public static string GetLanguageFromExtension(string extension)
  42.         {
  43.             CompilerInfo compilerInfo = (CompilerInfo)GetCompilerInfoForExtensionNoThrow(extension);
  44.             if (compilerInfo == null)
  45.                 throw new ConfigurationErrorsException(SR.GetString(SR.CodeDomProvider_NotDefined));
  46.             return compilerInfo._compilerLanguages[0];
  47.             // Return the first language name. There has to be one.
  48.         }
  49.        
  50.         [ComVisible(false)]
  51.         public static bool IsDefinedLanguage(string language)
  52.         {
  53.             return (GetCompilerInfoForLanguageNoThrow(language) != null);
  54.         }
  55.        
  56.         [ComVisible(false)]
  57.         public static bool IsDefinedExtension(string extension)
  58.         {
  59.             return (GetCompilerInfoForExtensionNoThrow(extension) != null);
  60.         }
  61.        
  62.        
  63.         [ComVisible(false)]
  64.         public static CompilerInfo GetCompilerInfo(string language)
  65.         {
  66.             CompilerInfo compilerInfo = GetCompilerInfoForLanguageNoThrow(language);
  67.             if (compilerInfo == null)
  68.                 throw new ConfigurationErrorsException(SR.GetString(SR.CodeDomProvider_NotDefined));
  69.             return compilerInfo;
  70.         }
  71.        
  72.        
  73.         // Do argument validation but don't throw if there's no compiler defined for a language.
  74.         private static CompilerInfo GetCompilerInfoForLanguageNoThrow(string language)
  75.         {
  76.             if (language == null)
  77.                 throw new ArgumentNullException("language");
  78.            
  79.             CompilerInfo compilerInfo = (CompilerInfo)Config._compilerLanguages[language.Trim()];
  80.             return compilerInfo;
  81.         }
  82.        
  83.         // Do argument validation but don't throw if there's no compiler defined for a language.
  84.         private static CompilerInfo GetCompilerInfoForExtensionNoThrow(string extension)
  85.         {
  86.             if (extension == null)
  87.                 throw new ArgumentNullException("extension");
  88.            
  89.             CompilerInfo compilerInfo = (CompilerInfo)Config._compilerExtensions[extension.Trim()];
  90.             return compilerInfo;
  91.         }
  92.        
  93.         [ComVisible(false)]
  94.         public static CompilerInfo[] GetAllCompilerInfo()
  95.         {
  96.             ArrayList values = Config._allCompilerInfo;
  97.             CompilerInfo[] compilerInfos = (CompilerInfo[])values.ToArray(typeof(CompilerInfo));
  98.             return compilerInfos;
  99.         }
  100.        
  101.         // Don't cache the configuration since things are different for asp.net scenarios.
  102.         private static CodeDomCompilationConfiguration Config {
  103.             get {
  104.                 CodeDomCompilationConfiguration _configuration = (CodeDomCompilationConfiguration)PrivilegedConfigurationManager.GetSection(CodeDomCompilationConfiguration.sectionName);
  105.                 if (_configuration == null) {
  106.                     return CodeDomCompilationConfiguration.Default;
  107.                 }
  108.                 return _configuration;
  109.             }
  110.         }
  111.        
  112.         /// <devdoc>
  113.         /// <para>Retrieves the default extension to use when saving files using this code dom provider.</para>
  114.         /// </devdoc>
  115.         public virtual string FileExtension {
  116.             get { return string.Empty; }
  117.         }
  118.        
  119.         /// <devdoc>
  120.         /// <para>Returns flags representing language variations.</para>
  121.         /// </devdoc>
  122.         public virtual LanguageOptions LanguageOptions {
  123.             get { return LanguageOptions.None; }
  124.         }
  125.        
  126.         [Obsolete("Callers should not use the ICodeGenerator interface and should instead use the methods directly on the CodeDomProvider class. Those inheriting from CodeDomProvider must still implement this interface, and should exclude this warning or also obsolete this method.")]
  127.         public abstract ICodeGenerator CreateGenerator();
  128.        
  129.         #pragma warning disable 618
  130.         public virtual ICodeGenerator CreateGenerator(TextWriter output)
  131.         {
  132.             return CreateGenerator();
  133.         }
  134.        
  135.         public virtual ICodeGenerator CreateGenerator(string fileName)
  136.         {
  137.             return CreateGenerator();
  138.         }
  139.         #pragma warning restore 618
  140.        
  141.         [Obsolete("Callers should not use the ICodeCompiler interface and should instead use the methods directly on the CodeDomProvider class. Those inheriting from CodeDomProvider must still implement this interface, and should exclude this warning or also obsolete this method.")]
  142.         public abstract ICodeCompiler CreateCompiler();
  143.        
  144.         [Obsolete("Callers should not use the ICodeParser interface and should instead use the methods directly on the CodeDomProvider class. Those inheriting from CodeDomProvider must still implement this interface, and should exclude this warning or also obsolete this method.")]
  145.         public virtual ICodeParser CreateParser()
  146.         {
  147.             return null;
  148.         }
  149.        
  150.         /// <devdoc>
  151.         /// This method allows a code dom provider implementation to provide a different type converter
  152.         /// for a given data type. At design time, a designer may pass data types through this
  153.         /// method to see if the code dom provider wants to provide an additional converter.
  154.         /// As typical way this would be used is if the language this code dom provider implements
  155.         /// does not support all of the values of MemberAttributes enumeration, or if the language
  156.         /// uses different names (Protected instead of Family, for example). The default
  157.         /// implementation just calls TypeDescriptor.GetConverter for the given type.
  158.         /// </devdoc>
  159.         public virtual TypeConverter GetConverter(Type type)
  160.         {
  161.             return TypeDescriptor.GetConverter(type);
  162.         }
  163.        
  164.         /// <devdoc>
  165.         /// <para>
  166.         /// Creates an assembly based on options, with the information from the compile units
  167.         /// </para>
  168.         /// </devdoc>
  169.         public virtual CompilerResults CompileAssemblyFromDom(CompilerParameters options, params CodeCompileUnit[] compilationUnits)
  170.         {
  171.             return CreateCompilerHelper().CompileAssemblyFromDomBatch(options, compilationUnits);
  172.         }
  173.        
  174.         /// <devdoc>
  175.         /// <para>
  176.         /// Creates an assembly based on options, with the contents of the files
  177.         /// </para>
  178.         /// </devdoc>
  179.         public virtual CompilerResults CompileAssemblyFromFile(CompilerParameters options, params string[] fileNames)
  180.         {
  181.             return CreateCompilerHelper().CompileAssemblyFromFileBatch(options, fileNames);
  182.         }
  183.        
  184.         /// <devdoc>
  185.         /// <para>
  186.         /// Creates an assembly based on options, with the information from sources
  187.         /// </para>
  188.         /// </devdoc>
  189.         public virtual CompilerResults CompileAssemblyFromSource(CompilerParameters options, params string[] sources)
  190.         {
  191.             return CreateCompilerHelper().CompileAssemblyFromSourceBatch(options, sources);
  192.         }
  193.        
  194.         /// <devdoc>
  195.         /// <para>
  196.         /// Gets a value indicating whether
  197.         /// the specified value is a valid identifier for this language.
  198.         /// </para>
  199.         /// </devdoc>
  200.         public virtual bool IsValidIdentifier(string value)
  201.         {
  202.             return CreateGeneratorHelper().IsValidIdentifier(value);
  203.         }
  204.        
  205.         public virtual string CreateEscapedIdentifier(string value)
  206.         {
  207.             return CreateGeneratorHelper().CreateEscapedIdentifier(value);
  208.         }
  209.        
  210.         public virtual string CreateValidIdentifier(string value)
  211.         {
  212.             return CreateGeneratorHelper().CreateValidIdentifier(value);
  213.         }
  214.        
  215.         public virtual string GetTypeOutput(CodeTypeReference type)
  216.         {
  217.             return CreateGeneratorHelper().GetTypeOutput(type);
  218.         }
  219.        
  220.         public virtual bool Supports(GeneratorSupport generatorSupport)
  221.         {
  222.             return CreateGeneratorHelper().Supports(generatorSupport);
  223.         }
  224.        
  225.         /// <devdoc>
  226.         /// <para>
  227.         /// Generates code from the specified expression and
  228.         /// outputs it to the specified textwriter.
  229.         /// </para>
  230.         /// </devdoc>
  231.         public virtual void GenerateCodeFromExpression(CodeExpression expression, TextWriter writer, CodeGeneratorOptions options)
  232.         {
  233.             CreateGeneratorHelper().GenerateCodeFromExpression(expression, writer, options);
  234.         }
  235.        
  236.         /// <devdoc>
  237.         /// <para>
  238.         /// Outputs the language specific representaion of the CodeDom tree
  239.         /// refered to by statement, into writer.
  240.         /// </para>
  241.         /// </devdoc>
  242.         public virtual void GenerateCodeFromStatement(CodeStatement statement, TextWriter writer, CodeGeneratorOptions options)
  243.         {
  244.             CreateGeneratorHelper().GenerateCodeFromStatement(statement, writer, options);
  245.         }
  246.        
  247.         /// <devdoc>
  248.         /// <para>
  249.         /// Outputs the language specific representaion of the CodeDom tree
  250.         /// refered to by codeNamespace, into writer.
  251.         /// </para>
  252.         /// </devdoc>
  253.         public virtual void GenerateCodeFromNamespace(CodeNamespace codeNamespace, TextWriter writer, CodeGeneratorOptions options)
  254.         {
  255.             CreateGeneratorHelper().GenerateCodeFromNamespace(codeNamespace, writer, options);
  256.         }
  257.        
  258.         /// <devdoc>
  259.         /// <para>
  260.         /// Outputs the language specific representaion of the CodeDom tree
  261.         /// refered to by compileUnit, into writer.
  262.         /// </para>
  263.         /// </devdoc>
  264.         public virtual void GenerateCodeFromCompileUnit(CodeCompileUnit compileUnit, TextWriter writer, CodeGeneratorOptions options)
  265.         {
  266.             CreateGeneratorHelper().GenerateCodeFromCompileUnit(compileUnit, writer, options);
  267.         }
  268.        
  269.         /// <devdoc>
  270.         /// <para>
  271.         /// Outputs the language specific representaion of the CodeDom tree
  272.         /// refered to by codeType, into writer.
  273.         /// </para>
  274.         /// </devdoc>
  275.         public virtual void GenerateCodeFromType(CodeTypeDeclaration codeType, TextWriter writer, CodeGeneratorOptions options)
  276.         {
  277.             CreateGeneratorHelper().GenerateCodeFromType(codeType, writer, options);
  278.         }
  279.        
  280.         public virtual void GenerateCodeFromMember(CodeTypeMember member, TextWriter writer, CodeGeneratorOptions options)
  281.         {
  282.             throw new NotImplementedException(SR.GetString(SR.NotSupported_CodeDomAPI));
  283.         }
  284.        
  285.         /// <devdoc>
  286.         /// <para>
  287.         /// Compiles the given text stream into a CodeCompile unit.
  288.         /// </para>
  289.         /// </devdoc>
  290.         public virtual CodeCompileUnit Parse(TextReader codeStream)
  291.         {
  292.             return CreateParserHelper().Parse(codeStream);
  293.         }
  294.        
  295.         #pragma warning disable 618
  296.         private ICodeCompiler CreateCompilerHelper()
  297.         {
  298.             ICodeCompiler compiler = CreateCompiler();
  299.             if (compiler == null) {
  300.                 throw new NotImplementedException(SR.GetString(SR.NotSupported_CodeDomAPI));
  301.             }
  302.             return compiler;
  303.         }
  304.        
  305.         private ICodeGenerator CreateGeneratorHelper()
  306.         {
  307.             ICodeGenerator generator = CreateGenerator();
  308.             if (generator == null) {
  309.                 throw new NotImplementedException(SR.GetString(SR.NotSupported_CodeDomAPI));
  310.             }
  311.             return generator;
  312.         }
  313.        
  314.         private ICodeParser CreateParserHelper()
  315.         {
  316.             ICodeParser parser = CreateParser();
  317.             if (parser == null) {
  318.                 throw new NotImplementedException(SR.GetString(SR.NotSupported_CodeDomAPI));
  319.             }
  320.             return parser;
  321.         }
  322.         #pragma warning restore 618
  323.     }
  324. }

Developer Fusion