48 lines
No EOL
1.3 KiB
C#
48 lines
No EOL
1.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace ShiroginSDK.Runtime.Services.Base
|
|
{
|
|
/// <summary>
|
|
/// Simple service locator for ShiroginSDK.
|
|
/// Provides global access to registered service instances.
|
|
/// </summary>
|
|
public static class ServiceLocator
|
|
{
|
|
private static readonly Dictionary<Type, object> _services = new();
|
|
|
|
public static void Register<T>(T service) where T : class
|
|
{
|
|
var type = typeof(T);
|
|
if (_services.ContainsKey(type))
|
|
_services[type] = service;
|
|
else
|
|
_services.Add(type, service);
|
|
}
|
|
|
|
public static T Get<T>() where T : class
|
|
{
|
|
if (_services.TryGetValue(typeof(T), out var service))
|
|
return service as T;
|
|
|
|
throw new Exception($"[ShiroginSDK] Service not found: {typeof(T).Name}");
|
|
}
|
|
|
|
public static bool TryGet<T>(out T service) where T : class
|
|
{
|
|
if (_services.TryGetValue(typeof(T), out var obj))
|
|
{
|
|
service = obj as T;
|
|
return true;
|
|
}
|
|
|
|
service = null;
|
|
return false;
|
|
}
|
|
|
|
public static void Clear()
|
|
{
|
|
_services.Clear();
|
|
}
|
|
}
|
|
} |