Example #1
0
        //If all the switches are valid, validate the switch parameters against any parameter
        //validators decorated on the method.
        private void CheckSwitchValidators()
        {
            foreach (KeyValuePair <string, SpecifiedSwitchParameters> kvp in _specifiedSwitches)
            {
                SwitchMethodInfo switchMethodInfo = _switches[kvp.Key];
                if (switchMethodInfo.Validators.Count == 0)
                {
                    continue;
                }

                foreach (List <string> parameters in kvp.Value)
                {
                    foreach (BaseValidatorAttribute validator in switchMethodInfo.Validators)
                    {
                        if (validator.ParameterIndex >= 0)
                        {
                            if (validator.ParameterIndex < parameters.Count)
                            {
                                validator.Validate(parameters[validator.ParameterIndex]);
                            }
                        }
                        else
                        {
                            foreach (string parameter in parameters)
                            {
                                validator.Validate(parameter);
                            }
                        }
                    }
                }
            }
        }
Example #2
0
        //Retrieve all methods and properties decorated with ConsoleFX attributes and cache them in
        //local fields.
        private void LoadMemberMetadata()
        {
            MethodInfo[] methods = _programType.GetMethods();
            foreach (MethodInfo method in methods)
            {
                if (method.IsDefined(typeof(SwitchAttribute), true))
                {
                    CommandLine.ValidateMethodSignature(method, typeof(void), typeof(string[]));
                    object[] switchAttributes = method.GetCustomAttributes(typeof(SwitchAttribute), true);
                    foreach (SwitchAttribute switchAttribute in switchAttributes)
                    {
                        SwitchMethodInfo switchMethodInfo = new SwitchMethodInfo(method);

                        object[] groupUsageAttributes = method.GetCustomAttributes(typeof(SwitchUsageAttribute), true);
                        switchMethodInfo.ModeUsages.AddRange((SwitchUsageAttribute[])groupUsageAttributes);

                        object[] validatorAttributes = method.GetCustomAttributes(typeof(BaseValidatorAttribute), true);
                        switchMethodInfo.Validators.AddRange((BaseValidatorAttribute[])validatorAttributes);

                        _switches.Add(switchAttribute, switchMethodInfo);
                    }
                    continue;
                }

                object[] errorHandlerAttributes = method.GetCustomAttributes(typeof(ErrorHandlerAttribute), true);
                if (errorHandlerAttributes.Length > 0)
                {
                    foreach (ErrorHandlerAttribute errorHandlerAttribute in errorHandlerAttributes)
                    {
                        CommandLine.ValidateMethodSignature(method, typeof(void), typeof(Exception));
                        _errorHandlers.Add(errorHandlerAttribute, method);
                    }
                    continue;
                }

                object[] executionAttributes = method.GetCustomAttributes(typeof(ExecutionPointAttribute), true);
                if (executionAttributes.Length > 0)
                {
                    foreach (ExecutionPointAttribute executionAttribute in executionAttributes)
                    {
                        CommandLine.ValidateMethodSignature(method, typeof(int), typeof(string[]));
                        _executionPoints.Add(executionAttribute, method);
                    }
                    continue;
                }

                object[] commonValidatorAttributes = method.GetCustomAttributes(typeof(ValidatorAttribute), true);
                if (commonValidatorAttributes.Length > 0)
                {
                    CommandLine.ValidateMethodSignature(method, typeof(bool), typeof(string[]));
                    _validatorMethod = method;
                    continue;
                }

                object[] modeSetterAttribute = method.GetCustomAttributes(typeof(ModeSetterAttribute), true);
                if (modeSetterAttribute.Length > 0)
                {
                    CommandLine.ValidateMethodSignature(method, typeof(int), typeof(string[]));
                    _modeSetterMethod = method;
                    continue;
                }

                object[] usageAttributes = method.GetCustomAttributes(typeof(UsageAttribute), true);
                if (usageAttributes.Length > 0)
                {
                    CommandLine.ValidateMethodSignature(method, typeof(void));
                    _usageMethod = method;
                    continue;
                }
            }

            PropertyInfo[] properties = _programType.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                if (!property.IsDefined(typeof(ParameterPropertyAttribute), true))
                {
                    continue;
                }
                ParameterPropertyAttribute[] parameterPropertyAttributes = ReflectionHelper.GetCustomAttributes <ParameterPropertyAttribute>(property, true);
                foreach (ParameterPropertyAttribute parameterPropertyAttribute in parameterPropertyAttributes)
                {
                    Dictionary <int, ParameterPropertyInfo> subPair;
                    if (!_parameterProperties.TryGetValue(parameterPropertyAttribute.Mode, out subPair))
                    {
                        subPair = new Dictionary <int, ParameterPropertyInfo>();
                        _parameterProperties.Add(parameterPropertyAttribute.Mode, subPair);
                    }
                    if (!subPair.ContainsKey(parameterPropertyAttribute.Index))
                    {
                        ParameterPropertyInfo parameterPropertyInfo = new ParameterPropertyInfo(property);
                        parameterPropertyInfo.Validators.AddRange(ReflectionHelper.GetCustomAttributes <BaseValidatorAttribute>(property, true));
                        subPair.Add(parameterPropertyAttribute.Index, parameterPropertyInfo);
                    }
                }
            }
        }