public static class ServiceProviderExtension { public static T GetService<T>(this IServiceProvider provider) where T: class { return provider.GetService(typeof(T)) as T; }
public static K GetService<T, K>(this IServiceProvider provider) where T : class, K where K : class { return provider.GetService() as K; }
public static K GetService<K>(this IServiceProvider provider, Type type) where K : class { return provider.GetService(type) as K; }
public static K GetService<K>(this IServiceProvider provider, string type) where K : class { return provider.GetService(Type.GetType(type)) as K; } }
IServiceProvider Extension Method
I love extension methods, 'nuff said:
See more in the archives