You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.9 KiB
C#
67 lines
1.9 KiB
C#
using Microsoft.Extensions.DependencyInjection;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Estsh.Client.Common.ServiceUtil
|
|
{
|
|
public class ServiceProviderHelper
|
|
{
|
|
/// <summary>
|
|
/// 全局服务提供者
|
|
/// </summary>
|
|
public static IServiceProvider ServiceProvider { get; private set; } = null!;
|
|
|
|
/// <summary>
|
|
/// 初始化构建ServiceProvider对象
|
|
/// </summary>
|
|
/// <param name="serviceProvider"></param>
|
|
/// <exception cref="ArgumentNullException"></exception>
|
|
public static void InitServiceProvider(ServiceProvider serviceProvider)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(serviceProvider, nameof(serviceProvider));
|
|
|
|
ServiceProvider = serviceProvider;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取Form服务
|
|
/// </summary>
|
|
/// <param name="type"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="ArgumentException"></exception>
|
|
public static Form GetFormService(Type type)
|
|
{
|
|
var service = ServiceProvider.GetRequiredService(type);
|
|
if (service is Form fService)
|
|
{
|
|
return fService;
|
|
}
|
|
else
|
|
{
|
|
throw new ArgumentException($"{type.FullName} is not a Form");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取Form服务
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static T GetRequiredService<T>() where T : class
|
|
{
|
|
return ServiceProvider.GetRequiredService<T>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取服务
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static T GetService<T>() where T : class
|
|
{
|
|
return ServiceProvider.GetService<T>();
|
|
}
|
|
}
|
|
}
|