/// <summary> /// 添加命名服务 /// </summary> /// <param name="name">服务名称</param> public static IServiceCollection AddNamedTransient(this IServiceCollection services, string name, Type implementationType) => services.FluentAdd(ServiceDescriptor.Transient(NamedType.Get(name, implementationType), implementationType));
/// <summary> /// 添加命名服务 /// </summary> /// <param name="name">服务名称</param> public static IServiceCollection AddNamedTransient <TService>(this IServiceCollection services, string name) => services.FluentAdd(ServiceDescriptor.Transient(NamedType.Get(name, typeof(TService)), typeof(TService)));
/// <summary> /// 添加命名服务 /// </summary> /// <param name="name">服务名称</param> public static IServiceCollection AddNamedTransient(this IServiceCollection services, string name, Func <IServiceProvider, object> implementationFactory) => services.FluentAdd(ServiceDescriptor.Transient(NamedType.Get(name, null), implementationFactory));
/// <summary> /// 添加命名服务 /// </summary> /// <param name="name">服务名称</param> public static IServiceCollection AddNamedSingleton <TService>(this IServiceCollection services, string name) where TService : class => services.FluentAdd(ServiceDescriptor.Singleton(NamedType.Get(name, typeof(TService)), typeof(TService)));
/// <summary> /// 添加命名服务 /// </summary> /// <param name="name">服务名称</param> public static IServiceCollection AddNamedSingleton <TService>(this IServiceCollection services, string name, TService implementationInstance) => services.FluentAdd(ServiceDescriptor.Singleton(NamedType.Get(name, typeof(TService)), implementationInstance));
/// <summary> /// 获取命名服务 /// </summary> /// <param name="name">服务名称</param> public static IEnumerable <object> GetNamedServices(this IServiceProvider provider, string name, Type serviceType) => provider.GetServices(NamedType.Get(name, serviceType)).Where(serviceType.IsInstanceOfType).ToArray();
/// <summary> /// 获取命名服务 /// </summary> /// <param name="name">服务名称</param> public static IEnumerable <T> GetNamedServices <T>(this IServiceProvider provider, string name) => provider.GetServices(NamedType.Get(name, typeof(T))).OfType <T>().ToArray();
/// <summary> /// 获取命名服务 /// </summary> /// <param name="name">服务名称</param> public static T GetNamedService <T>(this IServiceProvider provider, string name) => (T)provider.GetService(NamedType.Get(name, typeof(T)));
/// <summary> /// 获取命名服务 /// </summary> /// <param name="name">服务名称</param> public static object GetRequiredNamedService(this IServiceProvider provider, string name, Type serviceType) => provider.GetRequiredService(NamedType.Get(name, serviceType));
/// <summary> /// 获取命名服务 /// </summary> /// <param name="name">服务名称</param> public static object GetNamedService(this IServiceProvider provider, string name) => provider.GetService(NamedType.Get(name));