Exemplo n.º 1
0
        public void Execute(object target)
        {
            var methods = target.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);

            for (var i = 0; i < methods.Length; i++)
            {
                var methodInfo = methods[i];
                var attributes = System.Attribute.GetCustomAttributes(methodInfo, attributeType);

                for (var j = 0; j < attributes.Length; j++)
                {
                    if (!(attributes[j] is SaveMethodAttribute attribute))
                    {
                        continue;
                    }

                    var alias = container.Alias.Nameof(methodInfo.ReturnType, attribute.Key);

                    var parameterInfo = methodInfo.GetParameters();
                    if (parameterInfo is null || parameterInfo.Length == 0)
                    {
                        // вызов метода без передачи параметров

                        container.Set(alias, () => methodInfo.Invoke(target, null));
                    }
Exemplo n.º 2
0
        public void Execute(object target)
        {
            var fields = target.GetType().GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);

            for (var i = 0; i < fields.Length; i++)
            {
                var fieldInfo  = fields[i];
                var attributes = System.Attribute.GetCustomAttributes(fieldInfo, attributeType);

                for (var j = 0; j < attributes.Length; j++)
                {
                    if (!(attributes[j] is SaveValueAttribute attribute))
                    {
                        continue;
                    }

                    var value = fieldInfo.GetValue(target);

                    if (value == null)
                    {
                        throw new IoCException($"Can't add null value to the dependency injection collection. Field: {target.GetType().FullName}.{fieldInfo.Name}; ");
                    }

                    var parameterAlias = string.Empty;
                    if (attribute.useTypeFromParameter)
                    {
                        parameterAlias = container.Alias.Nameof(fieldInfo.FieldType, attribute.key);
                        container.Set(parameterAlias, value);
                    }
                    if (attribute.useTypeFromValue)
                    {
                        var valueAlias = container.Alias.Nameof(value, attribute.key);

                        if (parameterAlias != valueAlias)
                        {
                            container.Set(valueAlias, value);
                        }
                    }
                    if (!attribute.useTypeFromParameter && !attribute.useTypeFromValue)
                    {
                        throw new IoCAttributeException($"Invalid attribute parameters. Can't ignore both type names from value and from parameter at the same time, otherwise the value will not be added to the dipendency injection collection. Check field attribute: {target.GetType().FullName}.{fieldInfo.Name}; ");
                    }
                }
            }
        }
Exemplo n.º 3
0
        public void Execute(object target)
        {
            if (!(target is IControl control))
            {
                return;
            }

            var type = target.GetType();

            InjectPresenter(control, type);

            var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);

            for (var i = 0; i < properties.Length; i++)
            {
                var propertyInfo = properties[i];
                var attributes   = Attribute.GetCustomAttributes(propertyInfo, attributeType);

                for (var j = 0; j < attributes.Length; j++)
                {
                    if (!(attributes[j] is ProduceAttribute attribute))
                    {
                        continue;
                    }

                    var value        = propertyInfo.GetValue(target, null);
                    var created      = value != null;
                    var propertyType = propertyInfo.PropertyType;

                    #region Instantiate value
                    try
                    {
                        if (!created)
                        {
                            value = Activator.CreateInstance(propertyType);
                        }
                    }
                    catch (Exception exception)
                    {
                        throw new IoCException($"Couldn't create value for property: '{type.FullName}.{propertyInfo.Name}'; Properties type must be a class not an abstraction or interface and must have method constructor withour paramenters. Inner exception: {exception.Message}");
                    }
                    #endregion

                    #region Set value to property
                    try
                    {
                        if (!created)
                        {
                            propertyInfo.SetValue(target, value, null);
                        }
                    }
                    catch (ArgumentException argumentException)
                    {
                        throw new IoCException($"Couldn't inject value to property. {argumentException.Message}; Property is missing setter. Inner exception: {target.GetType().FullName}.{propertyInfo.Name} ; ");
                    }
                    #endregion

                    #region Setup value and add to DI container
                    if (value is IControl)
                    {
                        (value as IControl).Name = valueContainer.Alias.NotNull(attribute.key);
                    }

                    if (attribute.saveValue)
                    {
                        valueContainer.Set(valueContainer.Alias.Nameof(propertyType, attribute.key), value);
                    }
                    #endregion

                    builder.Produce(value);
                }
            }
        }