From 48d745ff9a1233912771beb9f033a658c00ebb67 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Mon, 12 Jun 2023 22:12:30 +0800 Subject: [PATCH] refactoring to support c# 7.3 --- src/AutoInjectSystem.cs | 76 +++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/src/AutoInjectSystem.cs b/src/AutoInjectSystem.cs index 99025d2..3ae908b 100644 --- a/src/AutoInjectSystem.cs +++ b/src/AutoInjectSystem.cs @@ -41,43 +41,45 @@ namespace DCFApixels.DragonECS foreach (var item in _systemProoperties.Keys) _notInjected.Add(item); } - private static List GetAllPropertiesFor(Type type) + + private static void Do(Type type, List result) { const BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; + result.AddRange(type.GetFields(bindingFlags) + .Where(o => o.GetCustomAttribute() != null) + .Select(o => new InjectedField(o))); + result.AddRange(type.GetProperties(bindingFlags) + .Where(o => { + if (o.GetCustomAttribute() == null) + return false; +#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS + if (o.CanWrite == false) + throw new EcsAutoInjectionException($"{o.Name} property is cant write"); +#endif + return true; + }) + .Select(o => new InjectedProperty(o))); + result.AddRange(type.GetMethods(bindingFlags) + .Where(o => { + if (o.GetCustomAttribute() == null) + return false; +#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS + if (o.IsGenericMethod) + throw new EcsAutoInjectionException($"{o.Name} method is Generic"); + if (o.GetParameters().Length != 1) + throw new EcsAutoInjectionException($"{o.Name} method Arguments != 1"); +#endif + return true; + }) + .Select(o => new InjectedMethod(o))); + if (type.BaseType != null) + Do(type.BaseType, result); + } + + private static List GetAllPropertiesFor(Type type) + { List result = new List(); Do(type, result); - static void Do(Type type, List result) - { - result.AddRange(type.GetFields(bindingFlags) - .Where(o => o.GetCustomAttribute() != null) - .Select(o => new InjectedField(o))); - result.AddRange(type.GetProperties(bindingFlags) - .Where(o =>{ - if (o.GetCustomAttribute() == null) - return false; -#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if (o.CanWrite == false) - throw new EcsAutoInjectionException($"{o.Name} property is cant write"); -#endif - return true; - }) - .Select(o => new InjectedProperty(o))); - result.AddRange(type.GetMethods(bindingFlags) - .Where(o => { - if (o.GetCustomAttribute() == null) - return false; -#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if (o.IsGenericMethod) - throw new EcsAutoInjectionException($"{o.Name} method is Generic"); - if (o.GetParameters().Length != 1) - throw new EcsAutoInjectionException($"{o.Name} method Arguments != 1"); -#endif - return true; - }) - .Select(o => new InjectedMethod(o))); - if (type.BaseType != null) - Do(type.BaseType, result); - } return result; } public void Inject(Type fieldType, object obj) @@ -119,7 +121,7 @@ namespace DCFApixels.DragonECS } WarningMissedInjections(); _notInjected.Clear(); - _notInjected= null; + _notInjected = null; } private void WarningMissedInjections() @@ -155,7 +157,7 @@ namespace DCFApixels.DragonECS private bool _isPreInjectionComplete = false; public void PreInject(object obj) { - if(_pipeline == null) + if (_pipeline == null) { _injectQueue.Add(obj); return; @@ -205,8 +207,8 @@ namespace DCFApixels.DragonECS #region Utils internal interface IInjectedProperty { - public bool IsInjected { get; } - public Type PropertyType { get; } + bool IsInjected { get; } + Type PropertyType { get; } EcsInjectAttribute GetAutoInjectAttribute(); void Inject(object target, object value); }