/// <summary> /// Removes services injected dependency injection. /// </summary> private void RemoveServices() { IEnumerable <FieldInfo> nonPublicFields = GetType().GetAllFields(BindingFlags.Instance | BindingFlags.NonPublic); foreach (FieldInfo nonPublicField in nonPublicFields) { InjectableServiceAttribute injectableServiceAttribute = nonPublicField.GetCustomAttribute(typeof(InjectableServiceAttribute)) as InjectableServiceAttribute; if (injectableServiceAttribute != null) { //nonPublicField.SetValue(this, null); } } }
/// <summary> /// Initializes services using dependency injection. /// </summary> private void InjectServices() { IEnumerable <FieldInfo> nonPublicFields = GetType().GetAllFields(BindingFlags.Instance | BindingFlags.NonPublic).Reverse(); foreach (FieldInfo nonPublicField in nonPublicFields) { InjectableServiceAttribute injectableServiceAttribute = nonPublicField.GetCustomAttribute(typeof(InjectableServiceAttribute)) as InjectableServiceAttribute; if (injectableServiceAttribute != null) { IServiceModule service = unityServiceLocator.GetInstance(nonPublicField.FieldType); if (service != null) { nonPublicField.SetValue(this, service); } else { logger.Error($"The service '{nonPublicField.Name}' of '{GetType().Name}' does not exist."); } } } }