Example #1
0
        public override void SetMember(MemberInfo fieldOrProperty)
        {
            Type enumType = CommandLineReflectionUtility.GetFieldOrPropertyType(fieldOrProperty);

            if (!enumType.IsEnum)
            {
                throw new ApplicationException(
                          string.Format(
                              "Attribute {0} can only be applied to enumeration fields or properties.", typeof(CommandLineEnumArgumentAttribute).FullName));
            }
            ((CommandLineEnumArgument)Argument).EnumType = enumType;
        }
Example #2
0
        public new object Parse(string[] args)
        {
            base.Parse(args);
            object obj = Activator.CreateInstance(_argumentClass);

            foreach (DictionaryEntry entry in _arguments)
            {
                CommandLineArgument argument        = (CommandLineArgument)entry.Key;
                MemberInfo          fieldOrProperty = (MemberInfo)entry.Value;
                Type   memberType = CommandLineReflectionUtility.GetFieldOrPropertyType(fieldOrProperty);
                object value      = argument.ValueObject;
                if (argument is ICommandLinePartArgument)
                {
                    value = ((ICommandLinePartArgument)argument).Group.ValueObject;
                }

                if (memberType == typeof(bool))
                {
                    if (value == null)
                    {
                        throw new ApplicationException(string.Format("{0} {1}: Cannot convert null to System.Boolean. Use Nullable<Boolean> type for optional attributes without default values.", fieldOrProperty.MemberType, fieldOrProperty.Name));
                    }
                    else if (value is bool?)
                    {
                        value = ((bool?)value).Value;
                    }
                }

                if (value != null)
                {
                    try
                    {
                        CommandLineReflectionUtility.SetFieldOrPropertyValue(obj, fieldOrProperty, value);
                    }
                    catch (Exception e)
                    {
                        throw new ApplicationException(string.Format("Error setting value of {0} {1}: {2}", fieldOrProperty.MemberType, fieldOrProperty.Name, e.Message), e);
                    }
                }
            }
            return(obj);
        }