diff --git a/Engine.Core/Factory/TypeFactory.cs b/Engine.Core/Factory/TypeFactory.cs index 7ba226a..d58ff65 100644 --- a/Engine.Core/Factory/TypeFactory.cs +++ b/Engine.Core/Factory/TypeFactory.cs @@ -1,20 +1,32 @@ using System; +using System.Reflection; namespace Syntriax.Engine.Core.Factory; public static class TypeFactory { public static T Get(params object?[]? args) where T : class + => Get(typeof(T), args); + + public static Type Get(string typeName) + { + foreach (var type in Assembly.GetExecutingAssembly().GetTypes()) + if (type.FullName?.Equals(typeName, StringComparison.OrdinalIgnoreCase) ?? false) + return type; + throw new Exception(); + } + + public static T Get(Type type, params object?[]? args) where T : class { T? result; if (args is not null && args.Length != 0) - result = Activator.CreateInstance(typeof(T), args) as T; + result = Activator.CreateInstance(type, args) as T; else - result = Activator.CreateInstance(typeof(T)) as T; + result = Activator.CreateInstance(type) as T; if (result is null) - throw new Exception($"{typeof(T).Name} of type {typeof(T).Name} could not be created."); + throw new Exception($"{type.Name} of type {type.Name} could not be created."); return result; }