IServiceProvider Extension Method

By on 8/21/2009

I love extension methods, 'nuff said:
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; } }

See more in the archives