2 // System.Web.Compilation.BuildProvider
5 // Chris Toshok (toshok@ximian.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // (C) 2006-2009 Novell, Inc (http://www.novell.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
34 using System.CodeDom.Compiler;
35 using System.Collections;
36 using System.Collections.Generic;
37 using System.Collections.Specialized;
39 using System.Reflection;
40 using System.Web.Configuration;
41 using System.Web.Hosting;
42 using System.Web.Util;
44 namespace System.Web.Compilation
46 public abstract class BuildProvider
49 static Dictionary <string, Type> registeredBuildProviderTypes;
51 ArrayList ref_assemblies;
53 ICollection vpath_deps;
54 CompilationSection compilationSection;
58 CompilationSection CompilationConfig {
60 if (compilationSection == null)
61 compilationSection = WebConfigurationManager.GetWebApplicationSection ("system.web/compilation") as CompilationSection;
62 return compilationSection;
66 internal virtual string LanguageName {
67 get { return CompilationConfig.DefaultLanguage; }
70 protected BuildProvider()
72 ref_assemblies = new ArrayList ();
75 internal void SetVirtualPath (VirtualPath virtualPath)
80 internal virtual void GenerateCode ()
84 internal virtual IDictionary <string, bool> ExtractDependencies ()
89 public virtual void GenerateCode (AssemblyBuilder assemblyBuilder)
93 public virtual string GetCustomString (CompilerResults results)
98 protected CompilerType GetDefaultCompilerType ()
100 return BuildManager.GetDefaultCompilerTypeForLanguage (CompilationConfig.DefaultLanguage, CompilationConfig);
103 protected CompilerType GetDefaultCompilerTypeForLanguage (string language)
105 return BuildManager.GetDefaultCompilerTypeForLanguage (language, null);
108 public virtual Type GetGeneratedType (CompilerResults results)
113 public virtual BuildProviderResultFlags GetResultFlags (CompilerResults results)
115 return BuildProviderResultFlags.Default;
118 protected TextReader OpenReader ()
120 return OpenReader (VirtualPath);
123 protected TextReader OpenReader (string virtualPath)
125 Stream st = OpenStream (virtualPath);
126 return new StreamReader (st, WebEncoding.FileEncoding);
129 protected Stream OpenStream ()
131 return OpenStream (VirtualPath);
134 protected Stream OpenStream (string virtualPath)
136 // MS also throws a NullReferenceException here when not hosted.
137 return VirtualPathProvider.OpenFile (virtualPath);
140 public static void RegisterBuildProvider (string extension, Type providerType)
142 if (String.IsNullOrEmpty (extension))
143 throw new ArgumentException ("The string parameter 'extension' cannot be null or empty.", "extension");
145 if (providerType == null)
146 throw new ArgumentNullException ("providerType");
148 if (!typeof (BuildProvider).IsAssignableFrom (providerType))
149 throw new ArgumentException ("The parameter 'providerType' is invalid", "providerType");
151 BuildManager.AssertPreStartMethodsRunning ();
153 if (registeredBuildProviderTypes == null)
154 registeredBuildProviderTypes = new Dictionary <string, Type> (StringComparer.OrdinalIgnoreCase);
156 registeredBuildProviderTypes [extension] = providerType;
159 internal static Type GetProviderTypeForExtension (string extension)
161 if (String.IsNullOrEmpty (extension))
165 if (registeredBuildProviderTypes == null || !registeredBuildProviderTypes.TryGetValue (extension, out type) || type == null) {
166 var cs = WebConfigurationManager.GetSection ("system.web/compilation") as CompilationSection;
167 BuildProviderCollection bpcoll = cs != null ? cs.BuildProviders : null;
168 global::System.Web.Configuration.BuildProvider bpcfg = bpcoll != null ? bpcoll [extension] : null;
170 type = HttpApplication.LoadType (bpcfg.Type);
176 internal static BuildProvider GetProviderInstanceForExtension (string extension)
178 Type type = GetProviderTypeForExtension (extension);
182 return Activator.CreateInstance (type, null) as global::System.Web.Compilation.BuildProvider;
185 public virtual CompilerType CodeCompilerType {
186 get { return null; } // Documented to return null
189 protected ICollection ReferencedAssemblies {
190 get { return ref_assemblies; }
193 protected internal string VirtualPath {
194 get { return vpath != null ? vpath.Absolute : null; }
197 internal VirtualPath VirtualPathInternal {
198 get { return vpath; }
201 public virtual ICollection VirtualPathDependencies {
203 if (vpath_deps == null)
204 vpath_deps = new OneNullCollection ();
210 internal virtual CodeCompileUnit CodeUnit {
215 class OneNullCollection : ICollection {
220 public bool IsSynchronized {
221 get { return false; }
224 public object SyncRoot {
228 public void CopyTo (Array array, int index)
231 throw new ArgumentNullException ();
234 throw new ArgumentOutOfRangeException ();
237 throw new ArgumentException ();
239 int length = array.Length;
240 if (index >= length || index > length - 1)
241 throw new ArgumentException ();
243 array.SetValue (null, index);
246 public IEnumerator GetEnumerator ()