2 // System.Web.Compilation.BuildManager
5 // Chris Toshok (toshok@ximian.com)
6 // Gonzalo Paniagua Javier (gonzalo@novell.com)
8 // (C) 2006 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.
36 using System.CodeDom.Compiler;
37 using System.Collections;
39 using System.Reflection;
41 using System.Web.Configuration;
42 using System.Web.Hosting;
44 namespace System.Web.Compilation {
45 public sealed class BuildManager {
46 internal BuildManager ()
50 internal static void ThrowNoProviderException (string extension)
52 string msg = "No registered provider for extension '{0}'.";
53 throw new HttpException (String.Format (msg, extension));
56 public static object CreateInstanceFromVirtualPath (string virtualPath, Type requiredBaseType)
58 // virtualPath + Exists done in GetCompiledType()
59 if (requiredBaseType == null)
60 throw new NullReferenceException (); // This is what MS does, but from somewhere else.
63 Type type = GetCompiledType (virtualPath);
64 if (!requiredBaseType.IsAssignableFrom (type)) {
65 string msg = String.Format ("Type '{0}' does not inherit from '{1}'.",
66 type.FullName, requiredBaseType.FullName);
67 throw new HttpException (500, msg);
70 return Activator.CreateInstance (type, null);
74 public static BuildDependencySet GetCachedBuildDependencySet (HttpContext context, string virtualPath)
76 return null; // null is ok here until we store the dependency set in the Cache.
79 public static Assembly GetCompiledAssembly (string virtualPath)
81 BuildProvider provider;
82 CompilerParameters parameters;
84 AssemblyBuilder abuilder = GetAssemblyBuilder (virtualPath, out provider);
85 CompilerType ctype = provider.CodeCompilerType;
86 parameters = PrepareParameters (abuilder.TempFiles, virtualPath, ctype.CompilerParameters);
87 CompilerResults results = abuilder.BuildAssembly (virtualPath, parameters);
88 return results.CompiledAssembly;
91 static AssemblyBuilder GetAssemblyBuilder (string virtualPath, out BuildProvider provider)
93 if (virtualPath == null || virtualPath == "")
94 throw new ArgumentNullException ("virtualPath");
96 if (virtualPath [0] != '/')
97 throw new ArgumentException ("The virtual path is not rooted", "virtualPath");
99 if (!HostingEnvironment.VirtualPathProvider.FileExists (virtualPath))
100 throw new HttpException (String.Format ("The file '{0}' does not exist.", virtualPath));
102 object o = WebConfigurationManager.GetSection ("system.web/compilation/buildProviders", virtualPath);
103 BuildProviderCollection coll = (BuildProviderCollection) o;
104 string extension = VirtualPathUtility.GetExtension (virtualPath);
105 if (coll == null || coll.Count == 0)
106 ThrowNoProviderException (extension);
108 provider = coll.GetProviderForExtension (extension);
109 if (provider == null)
110 ThrowNoProviderException (extension);
112 CompilerType compiler_type = provider.CodeCompilerType;
113 Type ctype = compiler_type.CodeDomProviderType;
114 CodeDomProvider dom_provider = (CodeDomProvider) Activator.CreateInstance (ctype, null);
116 AssemblyBuilder abuilder = new AssemblyBuilder (virtualPath, dom_provider);
117 provider.SetVirtualPath (virtualPath);
118 provider.GenerateCode (abuilder);
123 public static string GetCompiledCustomString (string virtualPath)
125 throw new NotImplementedException ();
128 static CompilerParameters PrepareParameters (TempFileCollection temp_files,
130 CompilerParameters base_params)
132 CompilerParameters res = new CompilerParameters ();
133 res.TempFiles = temp_files;
134 res.CompilerOptions = base_params.CompilerOptions;
135 res.IncludeDebugInformation = base_params.IncludeDebugInformation;
136 res.TreatWarningsAsErrors = base_params.TreatWarningsAsErrors;
137 res.WarningLevel = base_params.WarningLevel;
138 string dllfilename = Path.GetFileName (temp_files.AddExtension ("dll", true));
139 res.OutputAssembly = Path.Combine (temp_files.TempDir, dllfilename);
143 public static Type GetCompiledType (string virtualPath)
145 BuildProvider provider;
146 CompilerParameters parameters;
148 AssemblyBuilder abuilder = GetAssemblyBuilder (virtualPath, out provider);
149 CompilerType ctype = provider.CodeCompilerType;
150 parameters = PrepareParameters (abuilder.TempFiles, virtualPath, ctype.CompilerParameters);
151 CompilerResults results = abuilder.BuildAssembly (virtualPath, parameters);
152 return provider.GetGeneratedType (results);
155 // The 3 GetType() overloads work on the global.asax, App_GlobalResources, App_WebReferences or App_Browsers
157 public static Type GetType (string typeName, bool throwOnError)
159 throw new NotImplementedException ();
163 public static Type GetType (string typeName, bool throwOnError, bool ignoreCase)
165 throw new NotImplementedException ();
169 public static ICollection GetVirtualPathDependencies (string virtualPath)
171 throw new NotImplementedException ();
174 // Assemblies built from the App_Code directory
176 public static IList CodeAssemblies {
178 throw new NotImplementedException ();