Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Mist
- {
- /// <summary>
- /// Container class implements the IServiceProvider interface. This is used
- /// to pass shared services between different components, for instance the
- /// ContentManager uses it to locate the IGraphicsDeviceService implementation.
- /// </summary>
- public class ServiceContainer : IServiceProvider
- {
- /// <summary>
- /// A container that maps the type of service to an actual instance of the service.
- /// </summary>
- public Dictionary<Type, Object> Services = new Dictionary<Type, Object>();
- /// <summary>
- /// Adds a new service to the collection.
- /// </summary>
- /// <typeparam name="T">The type of parameter to be added.</typeparam>
- /// <param name="service">The service instance to be added.</param>
- public void AddService<T>(T service)
- {
- // Add the service to the container by type
- this.Services.Add(typeof(T), service);
- }
- /// <summary>
- /// Looks up the specified service.
- /// </summary>
- /// <param name="serviceType">A type that represents the service type.</param>
- /// <returns>Returns the service (or null if it was not found).</returns>
- public Object GetService(Type serviceType)
- {
- // This will store the service
- Object service;
- // Attempt to get the service by type out of the services map
- this.Services.TryGetValue(serviceType, out service);
- // Return the service
- return service;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement