Find all dependency properties in the class and inject the dependency if available
50 Type type = typeof(T);
53 if (type.Assembly.FullName.Contains(
"empeld"))
55 throw new AccessViolationException(
"DEBUG: Don't use dependency injection in empeld core");
59 var propDependencies = type
60 .GetProperties(DEPN_BINDING)
61 .Where(x => Attribute.IsDefined(x, typeof(DependencyAttribute)));
63 foreach (var target
in propDependencies)
65 if (target.GetValue(cls, null) == null)
67 var attr = (DependencyAttribute)target.GetCustomAttributes(typeof(DependencyAttribute),
true).Single();
68 var injectionResource = Game.Services.GetDependency(type.Assembly, target.PropertyType);
69 if (injectionResource != null)
71 PropertyInfo declarer = target;
72 if (declarer.DeclaringType != type)
74 declarer = target.DeclaringType.GetProperty(target.Name, DEPN_BINDING);
76 declarer.SetValue(cls, injectionResource, null);
78 else if(!attr.Optional)
80 throw new Exception(
string.Format(
"Failed to inject dependency for {0}. Dependency doesn't exist, or doesn't exist on both client and server.", target.PropertyType));
86 var fieldDependencies = type.GetFields(DEPN_BINDING)
87 .Where(x => Attribute.IsDefined(x, typeof(DependencyAttribute)));
89 foreach(var target
in fieldDependencies)
91 if (target.GetValue(cls) == null)
93 var attr = (DependencyAttribute)target.GetCustomAttributes(typeof(DependencyAttribute),
true).Single();
94 var injection = Game.Services.GetDependency(type.Assembly, target.FieldType);
95 if (injection != null)
97 FieldInfo declarer = target;
98 if (declarer.DeclaringType != type)
100 declarer = target.DeclaringType.GetField(target.Name, DEPN_BINDING);
102 declarer.SetValue(cls, injection);
104 else if(!attr.Optional)
106 throw new Exception(
string.Format(
"Failed to inject field dependency for {0}. Dependency doesn't exist, or doesn't exist on both client and server.", target.FieldType));