mirror of
https://github.com/DCFApixels/DragonECS-AutoInjections.git
synced 2025-09-17 20:34:34 +08:00
add named injection
This commit is contained in:
parent
435b5ac71d
commit
89cec06ae1
@ -5,11 +5,22 @@ namespace DCFApixels.DragonECS
|
|||||||
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
|
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
|
||||||
public class DIAttribute : Attribute
|
public class DIAttribute : Attribute
|
||||||
{
|
{
|
||||||
public static readonly DIAttribute Dummy = new DIAttribute(null);
|
public static readonly DIAttribute Dummy = new DIAttribute();
|
||||||
public readonly Type notNullDummyType;
|
public readonly Type NotNullDummyType = null;
|
||||||
public DIAttribute(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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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<EcsInjectAttribute>() != null;
|
|
||||||
//}
|
|
||||||
private static void Do(Type type, List<IInjectedProperty> result, bool isAgressiveInjection)
|
private static void Do(Type type, List<IInjectedProperty> result, bool isAgressiveInjection)
|
||||||
{
|
{
|
||||||
const BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
const BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
||||||
@ -110,11 +102,20 @@ namespace DCFApixels.DragonECS
|
|||||||
|
|
||||||
if (_systemProperties.TryGetValue(fieldType, out List<InjectedPropertyRecord> list))
|
if (_systemProperties.TryGetValue(fieldType, out List<InjectedPropertyRecord> list))
|
||||||
{
|
{
|
||||||
|
string name = string.Empty;
|
||||||
|
if(obj is INamedMember named)
|
||||||
|
{
|
||||||
|
name = named.Name;
|
||||||
|
}
|
||||||
foreach (var item in list)
|
foreach (var item in list)
|
||||||
|
{
|
||||||
|
string propertyName = item.Attribute.NamedInjection;
|
||||||
|
if (string.IsNullOrEmpty(propertyName) || propertyName == name)
|
||||||
{
|
{
|
||||||
item.property.Inject(item.target, obj);
|
item.property.Inject(item.target, obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -134,16 +135,16 @@ namespace DCFApixels.DragonECS
|
|||||||
{
|
{
|
||||||
foreach (var systemRecord in _systemProperties[notInjectedItem])
|
foreach (var systemRecord in _systemProperties[notInjectedItem])
|
||||||
{
|
{
|
||||||
if (systemRecord.Attribute.notNullDummyType == null)
|
if (systemRecord.Attribute.NotNullDummyType == null)
|
||||||
continue;
|
continue;
|
||||||
if (systemRecord.property.IsInjected)
|
if (systemRecord.property.IsInjected)
|
||||||
continue;
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
systemRecord.property.Inject(systemRecord.target, DummyInstance.GetInstance(systemRecord.Attribute.notNullDummyType));
|
systemRecord.property.Inject(systemRecord.target, DummyInstance.GetInstance(systemRecord.Attribute.NotNullDummyType));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
WarningMissedInjections();
|
WarningMissedInjections();
|
||||||
|
Loading…
Reference in New Issue
Block a user