mirror of
https://github.com/DCFApixels/DragonECS-AutoInjections.git
synced 2025-09-18 05:04:35 +08:00
add BindWithRunner
This commit is contained in:
parent
b801139e1c
commit
a7aa8b0113
10
src/AutoRunners/BindWithRunnerAttribute.cs
Normal file
10
src/AutoRunners/BindWithRunnerAttribute.cs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
using System;
|
||||||
|
[AttributeUsage(AttributeTargets.Interface, Inherited = false, AllowMultiple = false)]
|
||||||
|
sealed class BindWithRunnerAttribute : Attribute
|
||||||
|
{
|
||||||
|
public readonly Type runnerType;
|
||||||
|
public BindWithRunnerAttribute(Type runnerType)
|
||||||
|
{
|
||||||
|
this.runnerType = runnerType;
|
||||||
|
}
|
||||||
|
}
|
37
src/AutoRunners/ProcessRunnerBinder.cs
Normal file
37
src/AutoRunners/ProcessRunnerBinder.cs
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Security;
|
||||||
|
|
||||||
namespace DCFApixels.DragonECS
|
namespace DCFApixels.DragonECS
|
||||||
{
|
{
|
||||||
@ -24,6 +25,11 @@ namespace DCFApixels.DragonECS
|
|||||||
//method X has arguments greater than 1.
|
//method X has arguments greater than 1.
|
||||||
throw new EcsAutoInjectionException($"{obj.Name} method Arguments != 1");
|
throw new EcsAutoInjectionException($"{obj.Name} method Arguments != 1");
|
||||||
}
|
}
|
||||||
|
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||||
|
internal static void UndefinedException()
|
||||||
|
{
|
||||||
|
throw new Exception();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
22
src/Utils/ReflectionExtenions.cs
Normal file
22
src/Utils/ReflectionExtenions.cs
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
using System;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
|
namespace DCFApixels.DragonECS.AutoInjections.Internal
|
||||||
|
{
|
||||||
|
internal static class ReflectionExtenions
|
||||||
|
{
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static bool TryGetCustomAttribute<T>(this Type self, out T attribute) where T : Attribute
|
||||||
|
{
|
||||||
|
attribute = self.GetCustomAttribute<T>();
|
||||||
|
return attribute != null;
|
||||||
|
}
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static bool TryGetCustomAttribute<T>(this MemberInfo self, out T attribute) where T : Attribute
|
||||||
|
{
|
||||||
|
attribute = self.GetCustomAttribute<T>();
|
||||||
|
return attribute != null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user