From 86b920651510ff7becab288bddd971f466f46cd9 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Sat, 10 Feb 2024 17:10:24 +0300 Subject: [PATCH] feat: TypeFactory Methods --- Engine.Core/Factory/TypeFactory.cs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) 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; }