AspComponentFoundry.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // System.Web.Compilation.AspComponentFoundry
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  8. //
  9. using System;
  10. using System.Collections;
  11. using System.IO;
  12. using System.Reflection;
  13. namespace System.Web.Compilation
  14. {
  15. internal class AspComponentFoundry
  16. {
  17. private Hashtable foundries;
  18. public AspComponentFoundry ()
  19. {
  20. foundries = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
  21. CaseInsensitiveComparer.Default);
  22. RegisterFoundry ("asp", "System.Web", "System.Web.UI.WebControls");
  23. }
  24. public AspComponent MakeAspComponent (string foundryName, string componentName, Tag tag)
  25. {
  26. InternalFoundry foundry = foundries [foundryName] as InternalFoundry;
  27. if (foundry == null)
  28. throw new ApplicationException ("Foundry not found: " + foundryName);
  29. return new AspComponent (tag, foundry.GetType (componentName));
  30. }
  31. public void RegisterFoundry (string foundryName,
  32. string assemblyName,
  33. string nameSpace)
  34. {
  35. InternalFoundry foundry = new InternalFoundry (assemblyName, nameSpace, null);
  36. foundries.Add (foundryName, foundry);
  37. }
  38. public void RegisterFoundry (string foundryName,
  39. string assemblyName,
  40. string nameSpace,
  41. string typeName)
  42. {
  43. InternalFoundry foundry = new InternalFoundry (assemblyName, nameSpace, typeName);
  44. foundries.Add (foundryName, foundry);
  45. }
  46. public bool LookupFoundry (string foundryName)
  47. {
  48. return foundries.Contains (foundryName);
  49. }
  50. class InternalFoundry
  51. {
  52. string nameSpace;
  53. string assemblyName;
  54. string typeName;
  55. Assembly assembly;
  56. public InternalFoundry (string assemblyName, string nameSpace, string typeName)
  57. {
  58. this.assemblyName = assemblyName;
  59. this.nameSpace = nameSpace;
  60. this.typeName = typeName;
  61. assembly = null;
  62. }
  63. public Type GetType (string componentName)
  64. {
  65. EnsureAssembly ();
  66. // For ascx files
  67. if (typeName != null && 0 == String.Compare (componentName, typeName, true)) {
  68. throw new ApplicationException ("Only type '" + typeName + "' allowed.");
  69. } else if (typeName != null) {
  70. componentName = typeName;
  71. }
  72. return assembly.GetType (nameSpace + "." + componentName, true, true);
  73. }
  74. private void EnsureAssembly ()
  75. {
  76. if (assembly != null)
  77. return;
  78. try {
  79. assembly = Assembly.LoadFrom (Path.GetFullPath (assemblyName));
  80. } catch {
  81. assembly = Assembly.LoadWithPartialName (assemblyName);
  82. }
  83. if (assembly == null)
  84. throw new ApplicationException ("Assembly not found:" + assemblyName);
  85. }
  86. }
  87. }
  88. }