40 lines
873 B
C#
40 lines
873 B
C#
using Microsoft.Extensions.DependencyInjection;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Cowain.Base.Helpers;
|
|
|
|
/// <summary>
|
|
/// 服务定位器
|
|
/// </summary>
|
|
public class ServiceLocator
|
|
{
|
|
private static IServiceProvider? _current;
|
|
|
|
public static IServiceProvider Current
|
|
{
|
|
get => _current!;
|
|
set
|
|
{
|
|
if (_current != null)
|
|
{
|
|
throw new InvalidOperationException("The service locator has already been set.");
|
|
}
|
|
_current = value;
|
|
}
|
|
}
|
|
|
|
public static T GetRequiredService<T>() where T : class
|
|
{
|
|
return (T)Current.GetRequiredService(typeof(T));
|
|
}
|
|
|
|
public static object GetRequiredService(Type T)
|
|
{
|
|
return Current.GetRequiredService(T);
|
|
}
|
|
}
|