Syntriax.Engine/Engine.Core/Factory/FactoryBase.cs

24 lines
673 B
C#
Raw Permalink Normal View History

2023-11-23 22:07:49 +03:00
using System;
using Syntriax.Engine.Core.Factory.Abstract;
namespace Syntriax.Engine.Core.Factory;
public abstract class FactoryBase<TInterface> : IFactory<TInterface>
where TInterface : class
{
public virtual T Get<T>(params object?[]? args) where T : class, TInterface
{
T? result;
if (args is not null && args.Length != 0)
result = Activator.CreateInstance(typeof(T), args) as T;
else
result = Activator.CreateInstance(typeof(T)) as T;
if (result is null)
throw new Exception($"{typeof(TInterface).Name} of type {typeof(T).Name} could not be created.");
return result;
}
}