private void Push(ParserPositionalParameterResult param, object target) { var propertyTarget = param.Parameter.PropertyTarget(target); var type = propertyTarget.GetType(); var prop = type.GetProperty(param.Parameter.PropertyName); if (prop == null) { throw new CommandLineParserException("Property mismatch: command does not contain property with name '" + param.Parameter.PropertyName + "'."); } if (param.Parameter.IsList) { if (prop.IsList()) { PushList(param, prop, propertyTarget, typeof(List<>)); } else if (prop.IsHashSet()) { PushList(param, prop, propertyTarget, typeof(HashSet<>)); } // TODO - others? Is there a more generic way to cope with this? } else { Push(propertyTarget, prop, param.Values.First()); } }
private void AddToCollection(ParserPositionalParameterResult param, object collection, Type concreteType, Type[] genericArgs) { var add = concreteType.GetMethod("Add"); TypeConverter typeConverter = TypeDescriptor.GetConverter(genericArgs.First()); foreach (var stringVal in param.Values) { var val = typeConverter.ConvertFromString(stringVal); add.Invoke(collection, new[] { val }); } }
private void PushList(ParserPositionalParameterResult param, PropertyInfo prop, object propertyTarget, Type listType) { // var listType = typeof(List<>); var genericArgs = prop.PropertyType.GetGenericArguments(); var concreteType = listType.MakeGenericType(genericArgs); var list = prop.GetValue(propertyTarget); if (list == null) { list = Activator.CreateInstance(concreteType); prop.SetValue(propertyTarget, list); } AddToCollection(param, list, concreteType, genericArgs); }