Exemplo n.º 1
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 InjectToAttribute attribute))
                    {
                        continue;
                    }

                    try
                    {
                        var value = container.Get(container.Alias.Nameof(fieldInfo.FieldType, attribute.key));
                        fieldInfo.SetValue(target, value);
                    }
                    catch (IoCValueNotFoundException valueNotFound)
                    {
                        throw new IoCException($"Couldn't inject value to field: {target.GetType().FullName}.{fieldInfo.Name}; ", valueNotFound);
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void InjectPresenter(IControl control, Type controlType)
        {
            if (string.IsNullOrEmpty(control.Name))
            {
                control.Name = controlsContainer.Alias.NotNull(control.Name);
            }

            if (control.Presenter is null)
            {
                try
                {
                    var alias = controlsContainer.Alias.Nameof(controlType, control.Name);
                    control.Presenter = (IPresenter)controlsContainer.Get(alias);
                }
                catch (IoCValueNotFoundException notFound)
                {
                    throw new MvpPresenterException($"Presenter was not added to the DI collection for specific control. {notFound.Message}");
                }
                catch (InvalidCastException)
                {
                    throw new MvpPresenterException($"Can't add class to the control instance as presenter which doesn't implement '{nameof( IPresenter )}' interface. Control alias: '{controlsContainer.Alias.Nameof( controlType, control.Name )}'; ");
                }
                catch (ArgumentException argumentException)
                {
                    throw new IoCException($"Couldn't inject presenter. Control class '{controlType.FullName}' doesn't implement setter for presenter. Inner exception: {argumentException.Message}; ");
                }
            }

            if (control.Presenter.Control is null)
            {
                control.Presenter.Control = control;
            }

            builder.Produce(control.Presenter);
        }
Exemplo n.º 3
0
        public void Execute(object target)
        {
            var properties = target.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);

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

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

                    try
                    {
                        var value = container.Get(container.Alias.Nameof(propertyInfo.PropertyType, attribute.key));
                        propertyInfo.SetValue(target, value, null);
                    }
                    catch (IoCValueNotFoundException valueNotFound)
                    {
                        throw new IoCException($"Couldn't inject value to property: {target.GetType().FullName}.{propertyInfo.Name}; ", valueNotFound);
                    }
                    catch (System.ArgumentException argumentException)
                    {
                        throw new IoCException($"Can't inject value to property. {argumentException.Message}; Property is missing setter. Inner exception: {target.GetType().FullName}.{propertyInfo.Name} ; ");
                    }
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gets the ViewModel for the <c>page</c> of type <c>T</c>.
        /// </summary>
        /// <typeparam name="T">
        /// The type of the page to retrieve the ViewModel for.
        /// </typeparam>
        public BaseViewModel Get <T>() where T : Page
        {
            var viewModelType = GetViewModelTypeForPage <T>();

            if (viewModelType is null)
            {
                throw new Exception($"No ViewModel found for page {typeof(T).Name}");
            }

            return((BaseViewModel)dependencyContainer.Get(viewModelType));
        }
Exemplo n.º 5
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 InjectToMethodAttribute attribute))
                    {
                        continue;
                    }

                    var parameterInfo = methodInfo.GetParameters();
                    if (parameterInfo != null && parameterInfo.Length > 0)
                    {
                        var keys           = attribute.keys;
                        var parameterAlias = new string[parameterInfo.Length];

                        for (var k = 0; k < parameterInfo.Length; k++)
                        {
                            parameterAlias[k] = container.Alias.Nameof(parameterInfo[k].ParameterType, keys[k]);
                        }

                        var parameters = new object[parameterAlias.Length];
                        try
                        {
                            for (var l = 0; l < parameterAlias.Length; l++)
                            {
                                parameters[l] = container.Get(parameterAlias[l]);
                            }
                        }
                        catch (IoCValueNotFoundException notFound)
                        {
                            throw new IoCValueNotFoundException($"Couldn't inject values to method. {notFound.Message} Check method attributes at {target.GetType().FullName}.{methodInfo.Name};");
                        }
                        methodInfo.Invoke(target, parameters);
                    }
                    else
                    {
#if DEBUG
                        throw new IoCException($"Method with attribute '{nameof( InjectToMethodAttribute )}' must have at least one parameter. Invalid DI method: '{target.GetType().FullName}.{methodInfo.Name}'; ");
#endif
                        // throw exception
                    }
                }
            }
        }
 protected override IAnime CreateShowAnime(IDependencyContainer dependencies)
 {
     return(dependencies.Get <IAnimePreset>().GetSubMenuOverlayShow(this));
 }
Exemplo n.º 7
0
 /// <summary>
 /// Get the dependency.
 /// </summary>
 /// <typeparam name="T">
 /// The interface or concrete type.
 /// </typeparam>
 public T Get<T>() where T : class
 {
     return _sharedDependencyContainer.Get<T>();
 }
Exemplo n.º 8
0
 protected override IAnime CreateHideAnime(IDependencyContainer dependencies)
 {
     return(dependencies.Get <IAnimePreset>().GetSubMenuOverlayPopupHide(this, () => container));
 }
Exemplo n.º 9
0
 protected override IAnime CreateHideAnime(IDependencyContainer dependencies)
 {
     return(dependencies.Get <IAnimePreset>()?.GetDefaultOverlayHide(this));
 }
Exemplo n.º 10
0
 protected override IAnime CreateShowAnime(IDependencyContainer dependencies)
 {
     return(dependencies.Get <IAnimePreset>()?.GetDefaultScreenShow(this));
 }