ServiceContainer.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // ServiceContainer.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. #endregion
  13. namespace WinFormsContentLoading
  14. {
  15. /// <summary>
  16. /// Container class implements the IServiceProvider interface. This is used
  17. /// to pass shared services between different components, for instance the
  18. /// ContentManager uses it to locate the IGraphicsDeviceService implementation.
  19. /// </summary>
  20. public class ServiceContainer : IServiceProvider
  21. {
  22. Dictionary<Type, object> services = new Dictionary<Type, object>();
  23. /// <summary>
  24. /// Adds a new service to the collection.
  25. /// </summary>
  26. public void AddService<T>(T service)
  27. {
  28. services.Add(typeof(T), service);
  29. }
  30. /// <summary>
  31. /// Looks up the specified service.
  32. /// </summary>
  33. public object GetService(Type serviceType)
  34. {
  35. object service;
  36. services.TryGetValue(serviceType, out service);
  37. return service;
  38. }
  39. }
  40. }