Ejemplo n.º 1
0
        internal AttributeConfiguration(IServiceProvider serviceProvider, IEnumerable <Type> optionTypes, object?instance = null)
        {
            _serviceProvider = serviceProvider;
            foreach (var type in optionTypes)
            {
                var typeInfo = type.GetTypeInfo();

                foreach (var methodVerbPair in typeInfo.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static).Select((x) => new KeyValuePair <MethodInfo, IVerb?>(x, GetVerbFromMethod(x))).Where((x) => x.Value != null))
                {
                    var parameterOptions = new List <IParameterValue>();
                    foreach (var parameter in methodVerbPair.Key.GetParameters())
                    {
                        var option = AttributeHandler.GetOption(this, methodVerbPair.Key.ToString(), parameter);
                        if (option == null)
                        {
                            if (parameter.HasDefaultValue)
                            {
                                parameterOptions.Add(new DefaultValueParameter(parameter));
                            }
                            else
                            {
                                throw new InvalidOperationException($"The parameter \"{parameter.Name}\" on \"{methodVerbPair.Key.DeclaringType.FullName + ":" + methodVerbPair.Key.Name}\" for the verb \"{methodVerbPair.Value.Name}\" has neither an option, nor a default value.");
                            }
                        }
                        else
                        {
                            parameterOptions.Add(new ParameterValueOption(parameter, option, methodVerbPair.Key));
                        }
                    }

                    _verbData.Add(methodVerbPair.Value, new VerbData(methodVerbPair.Value, methodVerbPair.Key, () => {
                        if (instance != null)
                        {
                            return(instance);
                        }

                        return(AttributeHandler.GetConstructor(typeInfo).Invoke(new object[0]));
                    }, parameterOptions));
                }
            }
        }
Ejemplo n.º 2
0
        internal AttributeConfiguration(IServiceProvider serviceProvider, IEnumerable <Type> optionTypes)
        {
            _serviceProvider = serviceProvider;
            foreach (var type in optionTypes)
            {
                var typeInfo = type.GetTypeInfo();
                var verb     = GetVerbFromType(type);

                //get options
                var options = new List <OptionProperty>();
                foreach (var property in typeInfo.GetProperties(BindingFlags.Public | BindingFlags.Instance))
                {
                    var option = AttributeHandler.GetOption(this, type.Name, property);
                    if (option != null)
                    {
                        options.Add(new OptionProperty(option, property));
                    }
                }

                _verbsData.Add(verb, new VerbData(verb, () => AttributeHandler.GetConstructor(typeInfo).Invoke(new object[0]), options));
            }
        }