ServiceContainer.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // System.ComponentModel.Design.ServiceContainer.cs
  3. //
  4. // Authors:
  5. // Martin Willemoes Hansen ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) 2003 Martin Willemoes Hansen
  9. // (C) 2003 Andreas Nahr
  10. //
  11. //
  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:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  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.
  30. //
  31. using System.Collections;
  32. namespace System.ComponentModel.Design
  33. {
  34. public sealed class ServiceContainer : IServiceContainer, IServiceProvider
  35. {
  36. private IServiceProvider parentProvider;
  37. private Hashtable services = new Hashtable ();
  38. public ServiceContainer()
  39. : this (null)
  40. {
  41. }
  42. public ServiceContainer (IServiceProvider parentProvider)
  43. {
  44. this.parentProvider = parentProvider;
  45. }
  46. public void AddService (Type serviceType, object serviceInstance)
  47. {
  48. AddService (serviceType, serviceInstance, false);
  49. }
  50. public void AddService (Type serviceType, ServiceCreatorCallback callback)
  51. {
  52. AddService (serviceType, callback, false);
  53. }
  54. public void AddService (Type serviceType,
  55. object serviceInstance,
  56. bool promote)
  57. {
  58. if (serviceType == null)
  59. throw new ArgumentNullException ("serviceType", "Cannot be null");
  60. if (promote)
  61. if (parentProvider != null)
  62. ((IServiceContainer)parentProvider.GetService(typeof(IServiceContainer))).AddService (serviceType, serviceInstance, promote);
  63. if (services.Contains (serviceType)) {
  64. throw new ArgumentException (string.Format ("The service {0} already exists in the service container.", serviceType.ToString()));
  65. }
  66. services.Add (serviceType, serviceInstance);
  67. }
  68. public void AddService (Type serviceType,
  69. ServiceCreatorCallback callback,
  70. bool promote)
  71. {
  72. if (serviceType == null)
  73. throw new ArgumentNullException ("serviceType", "Cannot be null");
  74. if (promote)
  75. if (parentProvider != null)
  76. ((IServiceContainer)parentProvider.GetService(typeof(IServiceContainer))).AddService (serviceType, callback, promote);
  77. if (services.Contains (serviceType)) {
  78. throw new ArgumentException (string.Format ("The service {0} already exists in the service container.", serviceType.ToString()));
  79. }
  80. services.Add (serviceType, callback);
  81. }
  82. public void RemoveService (Type serviceType)
  83. {
  84. RemoveService (serviceType, false);
  85. }
  86. public void RemoveService (Type serviceType, bool promote)
  87. {
  88. if (serviceType == null)
  89. throw new ArgumentNullException ("serviceType", "Cannot be null");
  90. if (promote)
  91. if (parentProvider != null)
  92. ((IServiceContainer)parentProvider.GetService(typeof(IServiceContainer))).RemoveService (serviceType, promote);
  93. else
  94. services.Remove (serviceType);
  95. }
  96. public object GetService (Type serviceType)
  97. {
  98. object result = services[serviceType];
  99. if (result == null && parentProvider != null){
  100. result = parentProvider.GetService (serviceType);
  101. }
  102. if (result != null) {
  103. ServiceCreatorCallback cb = result as ServiceCreatorCallback;
  104. if (cb != null) {
  105. result = cb (this, serviceType);
  106. services[serviceType] = result;
  107. }
  108. }
  109. return result;
  110. }
  111. }
  112. }