BuildManager.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // System.Web.Compilation.BuildManager
  3. //
  4. // Authors:
  5. // Chris Toshok ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (C) 2006 Novell, Inc (http://www.novell.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. #if NET_2_0
  31. using System;
  32. using System.CodeDom;
  33. using System.CodeDom.Compiler;
  34. using System.Collections;
  35. using System.IO;
  36. using System.Reflection;
  37. using System.Web;
  38. using System.Web.Configuration;
  39. using System.Web.Hosting;
  40. namespace System.Web.Compilation {
  41. public sealed class BuildManager {
  42. internal BuildManager ()
  43. {
  44. }
  45. internal static void ThrowNoProviderException (string extension)
  46. {
  47. string msg = "No registered provider for extension '{0}'.";
  48. throw new HttpException (String.Format (msg, extension));
  49. }
  50. public static object CreateInstanceFromVirtualPath (string virtualPath, Type requiredBaseType)
  51. {
  52. // virtualPath + Exists done in GetCompiledType()
  53. if (requiredBaseType == null)
  54. throw new NullReferenceException (); // This is what MS does, but from somewhere else.
  55. // Get the Type.
  56. Type type = GetCompiledType (virtualPath);
  57. if (!requiredBaseType.IsAssignableFrom (type)) {
  58. string msg = String.Format ("Type '{0}' does not inherit from '{1}'.",
  59. type.FullName, requiredBaseType.FullName);
  60. throw new HttpException (500, msg);
  61. }
  62. return Activator.CreateInstance (type, null);
  63. }
  64. [MonoTODO]
  65. public static BuildDependencySet GetCachedBuildDependencySet (HttpContext context, string virtualPath)
  66. {
  67. return null; // null is ok here until we store the dependency set in the Cache.
  68. }
  69. public static Assembly GetCompiledAssembly (string virtualPath)
  70. {
  71. BuildProvider provider;
  72. CompilerParameters parameters;
  73. AssemblyBuilder abuilder = GetAssemblyBuilder (virtualPath, out provider);
  74. CompilerType ctype = provider.CodeCompilerType;
  75. parameters = PrepareParameters (abuilder.TempFiles, virtualPath, ctype.CompilerParameters);
  76. CompilerResults results = abuilder.BuildAssembly (virtualPath, parameters);
  77. return results.CompiledAssembly;
  78. }
  79. static AssemblyBuilder GetAssemblyBuilder (string virtualPath, out BuildProvider provider)
  80. {
  81. if (virtualPath == null || virtualPath == "")
  82. throw new ArgumentNullException ("virtualPath");
  83. if (virtualPath [0] != '/')
  84. throw new ArgumentException ("The virtual path is not rooted", "virtualPath");
  85. if (!HostingEnvironment.VirtualPathProvider.FileExists (virtualPath))
  86. throw new HttpException (String.Format ("The file '{0}' does not exist.", virtualPath));
  87. object o = WebConfigurationManager.GetSection ("system.web/compilation/buildProviders", virtualPath);
  88. BuildProviderCollection coll = (BuildProviderCollection) o;
  89. string extension = VirtualPathUtility.GetExtension (virtualPath);
  90. if (coll == null || coll.Count == 0)
  91. ThrowNoProviderException (extension);
  92. provider = coll.GetProviderForExtension (extension);
  93. if (provider == null)
  94. ThrowNoProviderException (extension);
  95. CompilerType compiler_type = provider.CodeCompilerType;
  96. Type ctype = compiler_type.CodeDomProviderType;
  97. CodeDomProvider dom_provider = (CodeDomProvider) Activator.CreateInstance (ctype, null);
  98. AssemblyBuilder abuilder = new AssemblyBuilder (virtualPath, dom_provider);
  99. provider.SetVirtualPath (virtualPath);
  100. provider.GenerateCode (abuilder);
  101. return abuilder;
  102. }
  103. [MonoTODO]
  104. public static string GetCompiledCustomString (string virtualPath)
  105. {
  106. throw new NotImplementedException ();
  107. }
  108. static CompilerParameters PrepareParameters (TempFileCollection temp_files,
  109. string virtualPath,
  110. CompilerParameters base_params)
  111. {
  112. CompilerParameters res = new CompilerParameters ();
  113. res.TempFiles = temp_files;
  114. res.CompilerOptions = base_params.CompilerOptions;
  115. res.IncludeDebugInformation = base_params.IncludeDebugInformation;
  116. res.TreatWarningsAsErrors = base_params.TreatWarningsAsErrors;
  117. res.WarningLevel = base_params.WarningLevel;
  118. string dllfilename = Path.GetFileName (temp_files.AddExtension ("dll", true));
  119. res.OutputAssembly = Path.Combine (temp_files.TempDir, dllfilename);
  120. return res;
  121. }
  122. public static Type GetCompiledType (string virtualPath)
  123. {
  124. BuildProvider provider;
  125. CompilerParameters parameters;
  126. AssemblyBuilder abuilder = GetAssemblyBuilder (virtualPath, out provider);
  127. CompilerType ctype = provider.CodeCompilerType;
  128. parameters = PrepareParameters (abuilder.TempFiles, virtualPath, ctype.CompilerParameters);
  129. CompilerResults results = abuilder.BuildAssembly (virtualPath, parameters);
  130. return provider.GetGeneratedType (results);
  131. }
  132. // The 3 GetType() overloads work on the global.asax, App_GlobalResources, App_WebReferences or App_Browsers
  133. [MonoTODO]
  134. public static Type GetType (string typeName, bool throwOnError)
  135. {
  136. throw new NotImplementedException ();
  137. }
  138. [MonoTODO]
  139. public static Type GetType (string typeName, bool throwOnError, bool ignoreCase)
  140. {
  141. throw new NotImplementedException ();
  142. }
  143. [MonoTODO]
  144. public static ICollection GetVirtualPathDependencies (string virtualPath)
  145. {
  146. throw new NotImplementedException ();
  147. }
  148. // Assemblies built from the App_Code directory
  149. [MonoTODO]
  150. public static IList CodeAssemblies {
  151. get {
  152. throw new NotImplementedException ();
  153. }
  154. }
  155. }
  156. }
  157. #endif