예제 #1
0
        private static void AssignValue(PropertyInfo property, ICommand command, string option, object value)
        {
            try {
                if (CommandLineUtility.IsMultiValuedProperty(property))
                {
                    // If we were able to look up a parent of type ICollection<>, perform a Add operation on it.
                    // Note that we expect the value is a string.
                    var stringValue = value as string;
                    Debug.Assert(stringValue != null);

                    dynamic list = property.GetValue(command, null);
                    // The parameter value is one or more semi-colon separated values: nuget pack -option "foo;bar;baz"
                    foreach (var item in stringValue.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        list.Add(item);
                    }
                }
                else
                {
                    property.SetValue(command, CommandLineUtility.ChangeType(value, property.PropertyType), null);
                }
            }
            catch {
                throw new CommandLineException(NuGetResources.InvalidOptionValueError, option, value);
            }
        }
예제 #2
0
        public IDictionary <OptionAttribute, PropertyInfo> GetCommandOptions(ICommand command)
        {
            var result = new Dictionary <OptionAttribute, PropertyInfo>();

            foreach (PropertyInfo propInfo in command.GetType().GetProperties())
            {
                foreach (OptionAttribute attr in propInfo.GetCustomAttributes(typeof(OptionAttribute), true))
                {
                    if (!propInfo.CanWrite && !CommandLineUtility.IsMultiValuedProperty(propInfo))
                    {
                        // If the property has neither a setter nor is of a type that can be cast to ICollection<> then there's no way to assign
                        // values to it. In this case throw.
                        throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
                                                                          NuGetResources.OptionInvalidWithoutSetter, command.GetType().FullName + "." + propInfo.Name));
                    }
                    result.Add(attr, propInfo);
                }
            }

            return(result);
        }