Advertisement
Guest User

Untitled

a guest
May 15th, 2012
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Mist
  5. {
  6.     /// <summary>
  7.     /// Container class implements the IServiceProvider interface. This is used
  8.     /// to pass shared services between different components, for instance the
  9.     /// ContentManager uses it to locate the IGraphicsDeviceService implementation.
  10.     /// </summary>
  11.     public class ServiceContainer : IServiceProvider
  12.     {
  13.         /// <summary>
  14.         /// A container that maps the type of service to an actual instance of the service.
  15.         /// </summary>
  16.         public Dictionary<Type, Object> Services = new Dictionary<Type, Object>();
  17.  
  18.         /// <summary>
  19.         /// Adds a new service to the collection.
  20.         /// </summary>
  21.         /// <typeparam name="T">The type of parameter to be added.</typeparam>
  22.         /// <param name="service">The service instance to be added.</param>
  23.         public void AddService<T>(T service)
  24.         {
  25.             // Add the service to the container by type
  26.             this.Services.Add(typeof(T), service);
  27.         }
  28.  
  29.         /// <summary>
  30.         /// Looks up the specified service.
  31.         /// </summary>
  32.         /// <param name="serviceType">A type that represents the service type.</param>
  33.         /// <returns>Returns the service (or null if it was not found).</returns>
  34.         public Object GetService(Type serviceType)
  35.         {
  36.             // This will store the service
  37.             Object service;
  38.  
  39.             // Attempt to get the service by type out of the services map
  40.             this.Services.TryGetValue(serviceType, out service);
  41.  
  42.             // Return the service
  43.             return service;
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement