diff --git a/Engine.Core/Factory/TypeFactory.cs b/Engine.Core/Factory/TypeFactory.cs index 7ba226a..54629c0 100644 --- a/Engine.Core/Factory/TypeFactory.cs +++ b/Engine.Core/Factory/TypeFactory.cs @@ -1,21 +1,34 @@ using System; +using System.Linq; +using System.Reflection; namespace Syntriax.Engine.Core.Factory; public static class TypeFactory { - public static T Get(params object?[]? args) where T : class + public static T Get(params object?[]? args) where T : class => (T)Get(typeof(T), args); + public static object Get(string fullName, params object?[]? args) => Get(GetType(fullName), args); + public static object Get(Type type, params object?[]? args) { - T? result; + object? result; if (args is not null && args.Length != 0) - result = Activator.CreateInstance(typeof(T), args) as T; + result = Activator.CreateInstance(type, args); else - result = Activator.CreateInstance(typeof(T)) as T; + result = Activator.CreateInstance(type); if (result is null) - throw new Exception($"{typeof(T).Name} of type {typeof(T).Name} could not be created."); + throw new Exception($"Type {type.Name} could not be created."); return result; } + + public static Type GetType(string fullName) + { + foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) + if (assembly.GetTypes().FirstOrDefault(t => t.FullName?.CompareTo(fullName) == 0) is Type type) + return type; + + throw new Exception($"Type {fullName} could not be found in the current domain."); + } }