DragonECS-AutoInjections/src/AutoRunners/ProcessRunnerBinder.cs

38 lines
1.3 KiB
C#
Raw Normal View History

2024-02-25 17:36:33 +08:00
using DCFApixels.DragonECS.AutoInjections.Internal;
using System;
using System.Reflection;
namespace DCFApixels.DragonECS
{
public static class ProcessRunnerBinder
{
private static MethodInfo _declareRunnerMethod = typeof(EcsPipeline).GetMethod(nameof(EcsPipeline.DeclareRunner));
public static T GetRunnerAuto<T>(this EcsPipeline self) where T : IEcsProcess
{
if(self.TryGetRunner(out T process))
{
return process;
}
Type type = typeof(T);
if (type.TryGetCustomAttribute(out BindWithRunnerAttribute atr))
{
Type runnerType = atr.runnerType;
if (type.IsGenericType)
{
if(runnerType.IsGenericType == false ||
runnerType.IsGenericTypeDefinition == false)
{
Throw.UndefinedException();
}
Type[] genericArguments = type.GetGenericArguments();
runnerType = runnerType.MakeGenericType(genericArguments);
}
return (T)_declareRunnerMethod.MakeGenericMethod(runnerType).Invoke(self, null);
}
Throw.UndefinedException();
return default;
}
}
}