Files
TheXamlGuy/Framework/Core/LifeCycles/ServiceFactory.cs
T
2022-11-01 15:26:08 +00:00

26 lines
743 B
C#

namespace TheXamlGuy.Framework.Core;
public class ServiceFactory : IServiceFactory
{
private readonly Func<Type, object?> factory;
private readonly Func<Type, object?[], object> creator;
public ServiceFactory(Func<Type, object?> factory, Func<Type, object?[], object> creator)
{
this.factory = factory;
this.creator = creator;
}
public T? Get<T>(Type type)
{
T? value = (T?)factory(type);
return value;
}
public T Create<T>(Type type, params object?[] parameters)
{
dynamic? lookup = factory(typeof(IServiceCreator<>).MakeGenericType(type));
return lookup is not null ? (T)lookup.Create(creator, parameters) : (T)creator(type, parameters);
}
}