27 lines
596 B
C#
27 lines
596 B
C#
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
|
||
|
namespace Notes.ViewModels;
|
||
|
|
||
|
public sealed class Services:IServices
|
||
|
{
|
||
|
private readonly Dictionary<Type, object?> map = new Dictionary<Type, object?>();
|
||
|
public void Register<T>(T t)
|
||
|
{
|
||
|
this.map.Add(typeof(T), t);
|
||
|
}
|
||
|
|
||
|
public T? Get<T>()
|
||
|
{
|
||
|
this.map.TryGetValue(typeof(T), out var t);
|
||
|
return (T?)t;
|
||
|
}
|
||
|
|
||
|
public T GetSafeAs<T>()
|
||
|
{
|
||
|
T? foo = this.Get<T?>();
|
||
|
if (foo is { } t)
|
||
|
return t;
|
||
|
throw new InvalidOperationException($"Not find type '{typeof(T)}'");
|
||
|
}
|
||
|
}
|