feat: TypeFactory Methods
This commit is contained in:
parent
0708ba89cc
commit
86b9206515
@ -1,20 +1,32 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
namespace Syntriax.Engine.Core.Factory;
|
namespace Syntriax.Engine.Core.Factory;
|
||||||
|
|
||||||
public static class TypeFactory
|
public static class TypeFactory
|
||||||
{
|
{
|
||||||
public static T Get<T>(params object?[]? args) where T : class
|
public static T Get<T>(params object?[]? args) where T : class
|
||||||
|
=> Get<T>(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<T>(Type type, params object?[]? args) where T : class
|
||||||
{
|
{
|
||||||
T? result;
|
T? result;
|
||||||
|
|
||||||
if (args is not null && args.Length != 0)
|
if (args is not null && args.Length != 0)
|
||||||
result = Activator.CreateInstance(typeof(T), args) as T;
|
result = Activator.CreateInstance(type, args) as T;
|
||||||
else
|
else
|
||||||
result = Activator.CreateInstance(typeof(T)) as T;
|
result = Activator.CreateInstance(type) as T;
|
||||||
|
|
||||||
if (result is null)
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user