diff --git a/src/Attributes/InjectionAttributes.cs b/src/Attributes/InjectionAttributes.cs index 9406041..5d4cc6b 100644 --- a/src/Attributes/InjectionAttributes.cs +++ b/src/Attributes/InjectionAttributes.cs @@ -5,11 +5,22 @@ namespace DCFApixels.DragonECS [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method, Inherited = false, AllowMultiple = false)] public class DIAttribute : Attribute { - public static readonly DIAttribute Dummy = new DIAttribute(null); - public readonly Type notNullDummyType; - public DIAttribute(Type notNullDummyType = null) + public static readonly DIAttribute Dummy = new DIAttribute(); + public readonly Type NotNullDummyType = null; + public readonly string NamedInjection = string.Empty; + public DIAttribute() { } + public DIAttribute(string namedInjection) { - this.notNullDummyType = notNullDummyType; + NamedInjection = namedInjection; + } + public DIAttribute(Type notNullDummyType) + { + NotNullDummyType = notNullDummyType; + } + public DIAttribute(string namedInjection, Type notNullDummyType) + { + NamedInjection = namedInjection; + NotNullDummyType = notNullDummyType; } } diff --git a/src/AutoInjectSystem.cs b/src/AutoInjectSystem.cs index c8ccc56..32997fa 100644 --- a/src/AutoInjectSystem.cs +++ b/src/AutoInjectSystem.cs @@ -42,16 +42,8 @@ namespace DCFApixels.DragonECS } } } - //foreach (var pair in _systemProperties) - //{ - // _notInjected.Add(pair.Key); - //} } - //private bool IsInjectTarget(MemberInfo member) - //{ - // return _isAgressiveInjection || member.GetCustomAttribute() != null; - //} private static void Do(Type type, List result, bool isAgressiveInjection) { const BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; @@ -110,9 +102,18 @@ namespace DCFApixels.DragonECS if (_systemProperties.TryGetValue(fieldType, out List list)) { + string name = string.Empty; + if(obj is INamedMember named) + { + name = named.Name; + } foreach (var item in list) { - item.property.Inject(item.target, obj); + string propertyName = item.Attribute.NamedInjection; + if (string.IsNullOrEmpty(propertyName) || propertyName == name) + { + item.property.Inject(item.target, obj); + } } } @@ -134,16 +135,16 @@ namespace DCFApixels.DragonECS { foreach (var systemRecord in _systemProperties[notInjectedItem]) { - if (systemRecord.Attribute.notNullDummyType == null) + if (systemRecord.Attribute.NotNullDummyType == null) continue; if (systemRecord.property.IsInjected) continue; - if (systemRecord.property.PropertyType.IsAssignableFrom(systemRecord.Attribute.notNullDummyType) == false) + if (systemRecord.property.PropertyType.IsAssignableFrom(systemRecord.Attribute.NotNullDummyType) == false) { - EcsDebug.Print(EcsConsts.DEBUG_ERROR_TAG, $"The {systemRecord.Attribute.notNullDummyType} dummy cannot be assigned to the {systemRecord.property.PropertyType.Name} field"); + EcsDebug.Print(EcsConsts.DEBUG_ERROR_TAG, $"The {systemRecord.Attribute.NotNullDummyType} dummy cannot be assigned to the {systemRecord.property.PropertyType.Name} field"); continue; } - systemRecord.property.Inject(systemRecord.target, DummyInstance.GetInstance(systemRecord.Attribute.notNullDummyType)); + systemRecord.property.Inject(systemRecord.target, DummyInstance.GetInstance(systemRecord.Attribute.NotNullDummyType)); } } WarningMissedInjections();