mscorlib(4.0.0.0) API with additions
CodeDomProvider.cs
1 using System.Collections;
6 using System.IO;
9 
11 {
13  [ToolboxItem(false)]
14  [ComVisible(true)]
15  [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
16  [PermissionSet(SecurityAction.InheritanceDemand, Name = "FullTrust")]
17  public abstract class CodeDomProvider : Component
18  {
19  private static CodeDomCompilationConfiguration Config
20  {
21  get
22  {
23  CodeDomCompilationConfiguration codeDomCompilationConfiguration = (CodeDomCompilationConfiguration)System.Configuration.PrivilegedConfigurationManager.GetSection("system.codedom");
24  if (codeDomCompilationConfiguration == null)
25  {
26  return CodeDomCompilationConfiguration.Default;
27  }
28  return codeDomCompilationConfiguration;
29  }
30  }
31 
34  public virtual string FileExtension => string.Empty;
35 
39 
44  [ComVisible(false)]
45  public static CodeDomProvider CreateProvider(string language, IDictionary<string, string> providerOptions)
46  {
47  CompilerInfo compilerInfo = GetCompilerInfo(language);
48  return compilerInfo.CreateProvider(providerOptions);
49  }
50 
57  [ComVisible(false)]
58  public static CodeDomProvider CreateProvider(string language)
59  {
60  CompilerInfo compilerInfo = GetCompilerInfo(language);
61  return compilerInfo.CreateProvider();
62  }
63 
70  [ComVisible(false)]
71  public static string GetLanguageFromExtension(string extension)
72  {
73  CompilerInfo compilerInfoForExtensionNoThrow = GetCompilerInfoForExtensionNoThrow(extension);
74  if (compilerInfoForExtensionNoThrow == null)
75  {
76  throw new ConfigurationErrorsException(SR.GetString("CodeDomProvider_NotDefined"));
77  }
78  return compilerInfoForExtensionNoThrow._compilerLanguages[0];
79  }
80 
87  [ComVisible(false)]
88  public static bool IsDefinedLanguage(string language)
89  {
90  return GetCompilerInfoForLanguageNoThrow(language) != null;
91  }
92 
99  [ComVisible(false)]
100  public static bool IsDefinedExtension(string extension)
101  {
102  return GetCompilerInfoForExtensionNoThrow(extension) != null;
103  }
104 
111  [ComVisible(false)]
112  public static CompilerInfo GetCompilerInfo(string language)
113  {
114  CompilerInfo compilerInfoForLanguageNoThrow = GetCompilerInfoForLanguageNoThrow(language);
115  if (compilerInfoForLanguageNoThrow == null)
116  {
117  throw new ConfigurationErrorsException(SR.GetString("CodeDomProvider_NotDefined"));
118  }
119  return compilerInfoForLanguageNoThrow;
120  }
121 
122  private static CompilerInfo GetCompilerInfoForLanguageNoThrow(string language)
123  {
124  if (language == null)
125  {
126  throw new ArgumentNullException("language");
127  }
128  return (CompilerInfo)Config._compilerLanguages[language.Trim()];
129  }
130 
131  private static CompilerInfo GetCompilerInfoForExtensionNoThrow(string extension)
132  {
133  if (extension == null)
134  {
135  throw new ArgumentNullException("extension");
136  }
137  return (CompilerInfo)Config._compilerExtensions[extension.Trim()];
138  }
139 
143  [ComVisible(false)]
145  {
146  ArrayList allCompilerInfo = Config._allCompilerInfo;
147  return (CompilerInfo[])allCompilerInfo.ToArray(typeof(CompilerInfo));
148  }
149 
152  [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.")]
153  public abstract ICodeGenerator CreateGenerator();
154 
159  {
160  return CreateGenerator();
161  }
162 
166  public virtual ICodeGenerator CreateGenerator(string fileName)
167  {
168  return CreateGenerator();
169  }
170 
173  [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.")]
174  public abstract ICodeCompiler CreateCompiler();
175 
178  [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.")]
179  public virtual ICodeParser CreateParser()
180  {
181  return null;
182  }
183 
187  public virtual TypeConverter GetConverter(Type type)
188  {
189  return TypeDescriptor.GetConverter(type);
190  }
191 
197  public virtual CompilerResults CompileAssemblyFromDom(CompilerParameters options, params CodeCompileUnit[] compilationUnits)
198  {
199  return CreateCompilerHelper().CompileAssemblyFromDomBatch(options, compilationUnits);
200  }
201 
207  public virtual CompilerResults CompileAssemblyFromFile(CompilerParameters options, params string[] fileNames)
208  {
209  return CreateCompilerHelper().CompileAssemblyFromFileBatch(options, fileNames);
210  }
211 
217  public virtual CompilerResults CompileAssemblyFromSource(CompilerParameters options, params string[] sources)
218  {
219  return CreateCompilerHelper().CompileAssemblyFromSourceBatch(options, sources);
220  }
221 
227  public virtual bool IsValidIdentifier(string value)
228  {
229  return CreateGeneratorHelper().IsValidIdentifier(value);
230  }
231 
236  public virtual string CreateEscapedIdentifier(string value)
237  {
238  return CreateGeneratorHelper().CreateEscapedIdentifier(value);
239  }
240 
245  public virtual string CreateValidIdentifier(string value)
246  {
247  return CreateGeneratorHelper().CreateValidIdentifier(value);
248  }
249 
254  public virtual string GetTypeOutput(CodeTypeReference type)
255  {
256  return CreateGeneratorHelper().GetTypeOutput(type);
257  }
258 
264  public virtual bool Supports(GeneratorSupport generatorSupport)
265  {
266  return CreateGeneratorHelper().Supports(generatorSupport);
267  }
268 
274  public virtual void GenerateCodeFromExpression(CodeExpression expression, TextWriter writer, CodeGeneratorOptions options)
275  {
276  CreateGeneratorHelper().GenerateCodeFromExpression(expression, writer, options);
277  }
278 
284  public virtual void GenerateCodeFromStatement(CodeStatement statement, TextWriter writer, CodeGeneratorOptions options)
285  {
286  CreateGeneratorHelper().GenerateCodeFromStatement(statement, writer, options);
287  }
288 
294  public virtual void GenerateCodeFromNamespace(CodeNamespace codeNamespace, TextWriter writer, CodeGeneratorOptions options)
295  {
296  CreateGeneratorHelper().GenerateCodeFromNamespace(codeNamespace, writer, options);
297  }
298 
304  public virtual void GenerateCodeFromCompileUnit(CodeCompileUnit compileUnit, TextWriter writer, CodeGeneratorOptions options)
305  {
306  CreateGeneratorHelper().GenerateCodeFromCompileUnit(compileUnit, writer, options);
307  }
308 
314  public virtual void GenerateCodeFromType(CodeTypeDeclaration codeType, TextWriter writer, CodeGeneratorOptions options)
315  {
316  CreateGeneratorHelper().GenerateCodeFromType(codeType, writer, options);
317  }
318 
324  public virtual void GenerateCodeFromMember(CodeTypeMember member, TextWriter writer, CodeGeneratorOptions options)
325  {
326  throw new NotImplementedException(SR.GetString("NotSupported_CodeDomAPI"));
327  }
328 
333  public virtual CodeCompileUnit Parse(TextReader codeStream)
334  {
335  return CreateParserHelper().Parse(codeStream);
336  }
337 
338  private ICodeCompiler CreateCompilerHelper()
339  {
340  ICodeCompiler codeCompiler = CreateCompiler();
341  if (codeCompiler == null)
342  {
343  throw new NotImplementedException(SR.GetString("NotSupported_CodeDomAPI"));
344  }
345  return codeCompiler;
346  }
347 
348  private ICodeGenerator CreateGeneratorHelper()
349  {
350  ICodeGenerator codeGenerator = CreateGenerator();
351  if (codeGenerator == null)
352  {
353  throw new NotImplementedException(SR.GetString("NotSupported_CodeDomAPI"));
354  }
355  return codeGenerator;
356  }
357 
358  private ICodeParser CreateParserHelper()
359  {
360  ICodeParser codeParser = CreateParser();
361  if (codeParser == null)
362  {
363  throw new NotImplementedException(SR.GetString("NotSupported_CodeDomAPI"));
364  }
365  return codeParser;
366  }
367 
368  internal static bool TryGetProbableCoreAssemblyFilePath(CompilerParameters parameters, out string coreAssemblyFilePath)
369  {
370  string text = null;
371  char[] separator = new char[1]
372  {
374  };
375  string value = Path.Combine("Reference Assemblies", "Microsoft", "Framework");
376  StringEnumerator enumerator = parameters.ReferencedAssemblies.GetEnumerator();
377  try
378  {
379  while (enumerator.MoveNext())
380  {
381  string current = enumerator.Current;
382  if (Path.GetFileName(current).Equals("mscorlib.dll", StringComparison.OrdinalIgnoreCase))
383  {
384  coreAssemblyFilePath = string.Empty;
385  return false;
386  }
387  if (current.IndexOf(value, StringComparison.OrdinalIgnoreCase) >= 0)
388  {
389  string[] array = current.Split(separator, StringSplitOptions.RemoveEmptyEntries);
390  for (int i = 0; i < array.Length - 5; i++)
391  {
392  if (string.Equals(array[i], "Reference Assemblies", StringComparison.OrdinalIgnoreCase) && array[i + 4].StartsWith("v", StringComparison.OrdinalIgnoreCase))
393  {
394  if (text != null)
395  {
396  if (!string.Equals(text, Path.GetDirectoryName(current), StringComparison.OrdinalIgnoreCase))
397  {
398  coreAssemblyFilePath = string.Empty;
399  return false;
400  }
401  }
402  else
403  {
404  text = Path.GetDirectoryName(current);
405  }
406  }
407  }
408  }
409  }
410  }
411  finally
412  {
413  (enumerator as IDisposable)?.Dispose();
414  }
415  if (text != null)
416  {
417  coreAssemblyFilePath = Path.Combine(text, "mscorlib.dll");
418  return true;
419  }
420  coreAssemblyFilePath = string.Empty;
421  return false;
422  }
423  }
424 }
LanguageOptions
Defines identifiers that indicate special features of a language.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
string Current
Gets the current element in the collection.
virtual ICodeGenerator CreateGenerator(string fileName)
When overridden in a derived class, creates a new code generator using the specified file name for ou...
virtual bool IsValidIdentifier(string value)
Returns a value that indicates whether the specified value is a valid identifier for the current lang...
string CreateEscapedIdentifier(string value)
Creates an escaped identifier for the specified value.
static string Combine(string path1, string path2)
Combines two strings into a path.
Definition: Path.cs:1107
virtual ICodeGenerator CreateGenerator(TextWriter output)
When overridden in a derived class, creates a new code generator using the specified T:System....
Represents the results of compilation that are returned from a compiler.
bool IsValidIdentifier(string value)
Gets a value that indicates whether the specified value is a valid identifier for the current languag...
CompilerResults CompileAssemblyFromDomBatch(CompilerParameters options, CodeCompileUnit[] compilationUnits)
Compiles an assembly based on the N:System.CodeDom trees contained in the specified array of T:System...
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
virtual TypeConverter GetConverter(Type type)
Gets a T:System.ComponentModel.TypeConverter for the specified data type.
static CodeDomProvider CreateProvider(string language)
Gets a T:System.CodeDom.Compiler.CodeDomProvider instance for the specified language.
static TypeConverter GetConverter(object component)
Returns a type converter for the type of the specified component.
virtual void GenerateCodeFromStatement(CodeStatement statement, TextWriter writer, CodeGeneratorOptions options)
Generates code for the specified Code Document Object Model (CodeDOM) statement and sends it to the s...
Represents a type declaration for a class, structure, interface, or enumeration.
Definition: __Canon.cs:3
void GenerateCodeFromStatement(CodeStatement e, TextWriter w, CodeGeneratorOptions o)
Generates code for the specified Code Document Object Model (CodeDOM) statement and outputs it to the...
Supports a simple iteration over a T:System.Collections.Specialized.StringCollection.
Defines an interface for parsing code into a T:System.CodeDom.CodeCompileUnit.
Definition: ICodeParser.cs:6
abstract ICodeCompiler CreateCompiler()
When overridden in a derived class, creates a new code compiler.
virtual void GenerateCodeFromCompileUnit(CodeCompileUnit compileUnit, TextWriter writer, CodeGeneratorOptions options)
Generates code for the specified Code Document Object Model (CodeDOM) compilation unit and sends it t...
CompilerResults CompileAssemblyFromFileBatch(CompilerParameters options, string[] fileNames)
Compiles an assembly from the source code contained within the specified files, using the specified c...
void GenerateCodeFromNamespace(CodeNamespace e, TextWriter w, CodeGeneratorOptions o)
Generates code for the specified Code Document Object Model (CodeDOM) namespace and outputs it to the...
static string GetFileName(string path)
Returns the file name and extension of the specified path string.
Definition: Path.cs:914
static readonly char DirectorySeparatorChar
Provides a platform-specific character used to separate directory levels in a path string that reflec...
Definition: Path.cs:17
Represents the parameters used to invoke a compiler.
static string GetLanguageFromExtension(string extension)
Returns a language name associated with the specified file name extension, as configured in the T:Sys...
virtual string FileExtension
Gets the default file name extension to use for source code files in the current language.
static bool IsDefinedExtension(string extension)
Tests whether a file name extension has an associated T:System.CodeDom.Compiler.CodeDomProvider imple...
CompilerResults CompileAssemblyFromSourceBatch(CompilerParameters options, string[] sources)
Compiles an assembly from the specified array of strings containing source code, using the specified ...
virtual CompilerResults CompileAssemblyFromSource(CompilerParameters options, params string[] sources)
Compiles an assembly from the specified array of strings containing source code, using the specified ...
SecurityAction
Specifies the security actions that can be performed using declarative security.
Defines an interface for invoking compilation of source code or a CodeDOM tree using a specific compi...
Definition: ICodeCompiler.cs:6
Defines an interface for generating code.
virtual bool Supports(GeneratorSupport generatorSupport)
Returns a value indicating whether the specified code generation support is provided.
string CreateValidIdentifier(string value)
Creates a valid identifier for the specified value.
void Dispose()
Releases all resources used by the T:System.ComponentModel.Component.
Definition: Component.cs:101
virtual void GenerateCodeFromType(CodeTypeDeclaration codeType, TextWriter writer, CodeGeneratorOptions options)
Generates code for the specified Code Document Object Model (CodeDOM) type declaration and sends it t...
virtual string CreateValidIdentifier(string value)
Creates a valid identifier for the specified value.
void GenerateCodeFromType(CodeTypeDeclaration e, TextWriter w, CodeGeneratorOptions o)
Generates code for the specified Code Document Object Model (CodeDOM) type declaration and outputs it...
static CompilerInfo GetCompilerInfo(string language)
Returns the language provider and compiler configuration settings for the specified language.
static CompilerInfo [] GetAllCompilerInfo()
Returns the language provider and compiler configuration settings for this computer.
Provides a base class for a member of a type. Type members include fields, methods,...
StringSplitOptions
Specifies whether applicable Overload:System.String.Split method overloads include or omit empty subs...
CodeCompileUnit Parse(TextReader codeStream)
When implemented in a derived class, compiles the specified text stream into a T:System....
Represents a writer that can write a sequential series of characters. This class is abstract.
Definition: TextWriter.cs:15
Represents a set of options used by a code generator.
Provides the base implementation for the T:System.ComponentModel.IComponent interface and enables obj...
Definition: Component.cs:9
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
Provides information about the characteristics for a component, such as its attributes,...
bool MoveNext()
Advances the enumerator to the next element of the collection.
virtual CompilerResults CompileAssemblyFromDom(CompilerParameters options, params CodeCompileUnit[] compilationUnits)
Compiles an assembly based on the N:System.CodeDom trees contained in the specified array of T:System...
CodeDomProvider CreateProvider()
Returns a T:System.CodeDom.Compiler.CodeDomProvider instance for the current language provider settin...
Definition: CompilerInfo.cs:96
void GenerateCodeFromExpression(CodeExpression e, TextWriter w, CodeGeneratorOptions o)
Generates code for the specified Code Document Object Model (CodeDOM) expression and outputs it to th...
static CodeDomProvider CreateProvider(string language, IDictionary< string, string > providerOptions)
Gets a T:System.CodeDom.Compiler.CodeDomProvider instance for the specified language and provider opt...
Provides a container for a CodeDOM program graph.
GeneratorSupport
Defines identifiers used to determine whether a code generator supports certain types of code element...
static bool IsDefinedLanguage(string language)
Tests whether a language has a T:System.CodeDom.Compiler.CodeDomProvider implementation configured on...
abstract ICodeGenerator CreateGenerator()
When overridden in a derived class, creates a new code generator.
virtual string CreateEscapedIdentifier(string value)
Creates an escaped identifier for the specified value.
virtual ICodeParser CreateParser()
When overridden in a derived class, creates a new code parser.
virtual CompilerResults CompileAssemblyFromFile(CompilerParameters options, params string[] fileNames)
Compiles an assembly from the source code contained in the specified files, using the specified compi...
Represents a namespace declaration.
virtual void GenerateCodeFromNamespace(CodeNamespace codeNamespace, TextWriter writer, CodeGeneratorOptions options)
Generates code for the specified Code Document Object Model (CodeDOM) namespace and sends it to the s...
Represents a code expression. This is a base class for other code expression objects that is never in...
Represents a reader that can read a sequential series of characters.
Definition: TextReader.cs:14
virtual CodeCompileUnit Parse(TextReader codeStream)
Compiles the code read from the specified text stream into a T:System.CodeDom.CodeCompileUnit.
Provides a base class for T:System.CodeDom.Compiler.CodeDomProvider implementations....
virtual void GenerateCodeFromMember(CodeTypeMember member, TextWriter writer, CodeGeneratorOptions options)
Generates code for the specified Code Document Object Model (CodeDOM) member declaration and sends it...
Represents the configuration settings of a language provider. This class cannot be inherited.
Definition: CompilerInfo.cs:10
Represents the abstract base class from which all code statements derive.
string GetTypeOutput(CodeTypeReference type)
Gets the type indicated by the specified T:System.CodeDom.CodeTypeReference.
static string GetDirectoryName(string path)
Returns the directory information for the specified path string.
Definition: Path.cs:268
bool Supports(GeneratorSupport supports)
Gets a value indicating whether the generator provides support for the language features represented ...
void GenerateCodeFromCompileUnit(CodeCompileUnit e, TextWriter w, CodeGeneratorOptions o)
Generates code for the specified Code Document Object Model (CodeDOM) compilation unit and outputs it...
virtual string GetTypeOutput(CodeTypeReference type)
Gets the type indicated by the specified T:System.CodeDom.CodeTypeReference.
Represents a reference to a type.
The exception that is thrown when a requested method or operation is not implemented.
Provides a unified way of converting types of values to other types, as well as for accessing standar...
Performs operations on T:System.String instances that contain file or directory path information....
Definition: Path.cs:13
virtual void GenerateCodeFromExpression(CodeExpression expression, TextWriter writer, CodeGeneratorOptions options)
Generates code for the specified Code Document Object Model (CodeDOM) expression and sends it to the ...
virtual object [] ToArray()
Copies the elements of the T:System.Collections.ArrayList to a new T:System.Object array.
Definition: ArrayList.cs:3083
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Definition: ArrayList.cs:14