예제 #1
0
        /// <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);
                }
            }
        }
예제 #2
0
        /// <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.");
                    }
                }
            }
        }