public static void ParseArguments(this object valueToPopulate, IEnumerable <string> args, char keyCharacter, char valueCharacter)
        {
            CommandLineDictionary        commandLineDictionary = CommandLineDictionary.FromArguments(args, keyCharacter, valueCharacter);
            PropertyDescriptorCollection properties            = TypeDescriptor.GetProperties(valueToPopulate);

            foreach (PropertyDescriptor propertyDescriptor in properties)
            {
                if (propertyDescriptor.Attributes.Cast <Attribute>().Any <Attribute>((Func <Attribute, bool>)(attribute => attribute is RequiredAttribute)) && !commandLineDictionary.ContainsKey(propertyDescriptor.Name))
                {
                    throw new ArgumentException("A value for the " + propertyDescriptor.Name + " property is required.");
                }
            }
            foreach (KeyValuePair <string, string> keyValuePair in (Dictionary <string, string>)commandLineDictionary)
            {
                PropertyDescriptor propertyDescriptor1 = (PropertyDescriptor)null;
                foreach (PropertyDescriptor propertyDescriptor2 in properties)
                {
                    if (string.Equals(propertyDescriptor2.Name, keyValuePair.Key, StringComparison.OrdinalIgnoreCase))
                    {
                        propertyDescriptor1 = propertyDescriptor2;
                    }
                }
                if (propertyDescriptor1 == null)
                {
                    throw new ArgumentException("A matching property of name " + keyValuePair.Key + " on type " + (object)valueToPopulate.GetType() + " could not be found.");
                }
                if (string.IsNullOrEmpty(keyValuePair.Value) && (propertyDescriptor1.PropertyType == typeof(bool) || propertyDescriptor1.PropertyType == typeof(bool?)))
                {
                    propertyDescriptor1.SetValue(valueToPopulate, (object)true);
                }
                else
                {
                    object obj;
                    switch (propertyDescriptor1.PropertyType.Name)
                    {
                    case "IEnumerable`1":
                    case "ICollection`1":
                    case "IList`1":
                    case "List`1":
                        obj = typeof(CommandLineParser).GetMethod("FromCommaSeparatedList", BindingFlags.Static | BindingFlags.NonPublic).MakeGenericMethod(propertyDescriptor1.PropertyType.GetGenericArguments()).Invoke((object)null, new object[1]
                        {
                            (object)keyValuePair.Value
                        });
                        break;

                    default:
                        TypeConverter converter = TypeDescriptor.GetConverter(propertyDescriptor1.PropertyType);
                        if (converter == null || !converter.CanConvertFrom(typeof(string)))
                        {
                            throw new ArgumentException("Unable to convert from a string to a property of type " + (object)propertyDescriptor1.PropertyType + ".");
                        }
                        obj = converter.ConvertFromInvariantString(keyValuePair.Value);
                        break;
                    }
                    propertyDescriptor1.SetValue(valueToPopulate, obj);
                }
            }
        }
        public static string ToString(object valueToConvert)
        {
            IEnumerable <PropertyDescriptor> propertyDescriptors   = TypeDescriptor.GetProperties(valueToConvert).Cast <PropertyDescriptor>().Except <PropertyDescriptor>(TypeDescriptor.GetProperties(valueToConvert.GetType().BaseType).Cast <PropertyDescriptor>());
            CommandLineDictionary            commandLineDictionary = new CommandLineDictionary();

            foreach (PropertyDescriptor propertyDescriptor in propertyDescriptors)
            {
                commandLineDictionary[propertyDescriptor.Name] = propertyDescriptor.GetValue(valueToConvert).ToString();
            }
            return(commandLineDictionary.ToString());
        }
Exemplo n.º 3
0
        public static CommandLineDictionary FromArguments(IEnumerable <string> arguments, char keyCharacter, char valueCharacter)
        {
            CommandLineDictionary commandLineDictionary = new CommandLineDictionary();

            commandLineDictionary.KeyCharacter   = keyCharacter;
            commandLineDictionary.ValueCharacter = valueCharacter;
            foreach (string str in arguments)
            {
                commandLineDictionary.AddArgument(str);
            }
            return(commandLineDictionary);
        }
Exemplo n.º 4
0
 public static CommandLineDictionary FromArguments(IEnumerable <string> arguments)
 {
     return(CommandLineDictionary.FromArguments(arguments, '/', '='));
 }