1 //------------------------------------------------------------------------------
2 // <copyright file="CompilerInfo.cs" company="Microsoft">
4 // <OWNER>Microsoft</OWNER>
5 // Copyright (c) Microsoft Corporation. All rights reserved.
7 //------------------------------------------------------------------------------
9 namespace System.CodeDom.Compiler {
11 using System.Reflection;
12 using System.Security.Permissions;
13 using System.CodeDom.Compiler;
14 using System.Configuration;
15 using System.Collections.Generic;
16 using System.Diagnostics;
18 [PermissionSet(SecurityAction.LinkDemand, Name="FullTrust")]
19 public sealed class CompilerInfo {
20 internal String _codeDomProviderTypeName; // This can never by null
21 internal CompilerParameters _compilerParams; // This can never by null
22 internal String[] _compilerLanguages; // This can never by null
23 internal String[] _compilerExtensions; // This can never by null
24 internal String configFileName;
25 internal IDictionary<string, string> _providerOptions; // This can never be null
26 internal int configFileLineNumber;
27 internal Boolean _mapped;
31 private CompilerInfo() {} // Not createable
33 public String[] GetLanguages() {
34 return CloneCompilerLanguages();
37 public String[] GetExtensions() {
38 return CloneCompilerExtensions();
41 public Type CodeDomProviderType {
46 type = Type.GetType(_codeDomProviderTypeName);
48 if( configFileName == null) {
49 throw new ConfigurationErrorsException(SR.GetString(SR.Unable_To_Locate_Type,
50 _codeDomProviderTypeName, string.Empty, 0));
53 throw new ConfigurationErrorsException(SR.GetString(SR.Unable_To_Locate_Type,
54 _codeDomProviderTypeName), configFileName, configFileLineNumber);
65 public bool IsCodeDomProviderTypeValid {
67 Type type = Type.GetType(_codeDomProviderTypeName);
68 return (type != null);
72 public CodeDomProvider CreateProvider() {
73 // if the provider defines an IDictionary<string, string> ctor and
74 // provider options have been provided then call that and give it the
75 // provider options dictionary. Otherwise call the normal one.
77 Debug.Assert(_providerOptions != null, "Created CompilerInfo w/ null _providerOptions");
79 if (_providerOptions.Count > 0) {
80 ConstructorInfo ci = CodeDomProviderType.GetConstructor(new Type[] { typeof(IDictionary<string, string>) });
82 return (CodeDomProvider)ci.Invoke(new object[] { _providerOptions });
86 return (CodeDomProvider)Activator.CreateInstance(CodeDomProviderType);
89 public CodeDomProvider CreateProvider(IDictionary<String, String> providerOptions)
91 if (providerOptions == null)
92 throw new ArgumentNullException("providerOptions");
94 ConstructorInfo constructor = CodeDomProviderType.GetConstructor(new Type[] { typeof(IDictionary<string, string>) });
95 if (constructor != null) {
96 return (CodeDomProvider)constructor.Invoke(new object[] { providerOptions });
99 throw new InvalidOperationException(SR.GetString(SR.Provider_does_not_support_options, CodeDomProviderType.ToString()));
103 public CompilerParameters CreateDefaultCompilerParameters() {
104 return CloneCompilerParameters();
108 internal CompilerInfo(CompilerParameters compilerParams, String codeDomProviderTypeName, String[] compilerLanguages, String[] compilerExtensions) {
109 _compilerLanguages = compilerLanguages;
110 _compilerExtensions = compilerExtensions;
111 _codeDomProviderTypeName = codeDomProviderTypeName;
112 if (compilerParams == null)
113 compilerParams = new CompilerParameters();
115 _compilerParams = compilerParams;
118 internal CompilerInfo(CompilerParameters compilerParams, String codeDomProviderTypeName) {
119 _codeDomProviderTypeName = codeDomProviderTypeName;
120 if (compilerParams == null)
121 compilerParams = new CompilerParameters();
123 _compilerParams = compilerParams;
127 public override int GetHashCode() {
128 return _codeDomProviderTypeName.GetHashCode();
131 public override bool Equals(Object o) {
132 CompilerInfo other = o as CompilerInfo;
136 return CodeDomProviderType == other.CodeDomProviderType &&
137 CompilerParams.WarningLevel == other.CompilerParams.WarningLevel &&
138 CompilerParams.IncludeDebugInformation == other.CompilerParams.IncludeDebugInformation &&
139 CompilerParams.CompilerOptions == other.CompilerParams.CompilerOptions;
142 private CompilerParameters CloneCompilerParameters() {
143 CompilerParameters copy = new CompilerParameters();
144 copy.IncludeDebugInformation = _compilerParams.IncludeDebugInformation;
145 copy.TreatWarningsAsErrors = _compilerParams.TreatWarningsAsErrors;
146 copy.WarningLevel = _compilerParams.WarningLevel;
147 copy.CompilerOptions = _compilerParams.CompilerOptions;
151 private String[] CloneCompilerLanguages() {
152 String[] compilerLanguages = new String[_compilerLanguages.Length];
153 Array.Copy(_compilerLanguages, compilerLanguages, _compilerLanguages.Length);
154 return compilerLanguages;
157 private String[] CloneCompilerExtensions() {
158 String[] compilerExtensions = new String[_compilerExtensions.Length];
159 Array.Copy(_compilerExtensions, compilerExtensions, _compilerExtensions.Length);
160 return compilerExtensions;
163 internal CompilerParameters CompilerParams {
165 return _compilerParams;
170 internal IDictionary<string, string> ProviderOptions {
172 return _providerOptions;