Virtu/Virtu/Services/MachineServices.cs
Sean Fausett acd7892436 Cosmetic changes.
Fixed some code analysis warnings.
Dropped Extended Strongly Typed Resource Generator dependency.

--HG--
extra : convert_revision : svn%3Affd33b8c-2492-42e0-bdc5-587b920b7d6d/trunk%4035615
2009-12-11 09:12:31 +00:00

52 lines
1.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using Jellyfish.Virtu.Properties;
namespace Jellyfish.Virtu.Services
{
public sealed class MachineServices : IServiceProvider
{
public void AddService(Type serviceType, MachineService serviceProvider)
{
if (serviceType == null)
{
throw new ArgumentNullException("serviceType");
}
if (_serviceProviders.ContainsKey(serviceType))
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, SR.ServiceAlreadyPresent, serviceType.FullName), "serviceType");
}
if (serviceProvider == null)
{
throw new ArgumentNullException("serviceProvider");
}
if (!serviceType.IsAssignableFrom(serviceProvider.GetType()))
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, SR.ServiceMustBeAssignable, serviceType.FullName, serviceProvider.GetType().FullName));
}
_serviceProviders.Add(serviceType, serviceProvider);
}
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
public T GetService<T>()
{
return (T)GetService(typeof(T));
}
public object GetService(Type serviceType)
{
return _serviceProviders.ContainsKey(serviceType) ? _serviceProviders[serviceType] : null;
}
public void RemoveService(Type serviceType)
{
_serviceProviders.Remove(serviceType);
}
private Dictionary<Type, MachineService> _serviceProviders = new Dictionary<Type, MachineService>();
}
}