public override object Convert(object source, Type sourceType, Type targetType) { if (source == null) { return(null); } var sourceString = source as string; var fields = sourceString.Split(_delimit, StringSplitOptions.RemoveEmptyEntries); var targetElementType = ConversionUtils.GetElementType(targetType); var list = ConversionUtils.CreateCompatListFor(targetType); if (list == null) { throw new InvalidOperationException("Unable to create compatable list"); } for (var i = 0; i < fields.Length; i++) { var sourceElement = fields[i]; var targetElement = _conversionService.Convert(sourceElement.Trim(), sourceType, targetElementType); list.Add(targetElement); } return(list); }
public override object Convert(object source, Type sourceType, Type targetType) { var sourceCollection = source as IEnumerable; if (sourceCollection == null) { return(null); } var list = ConversionUtils.CreateCompatListFor(targetType); if (list == null) { throw new InvalidOperationException("Unable to create compatible list"); } if (!targetType.IsGenericType) { foreach (var elem in sourceCollection) { list.Add(elem); } } else { var targetElementType = ConversionUtils.GetElementType(targetType) ?? typeof(object); foreach (var sourceElement in sourceCollection) { if (sourceElement != null) { var targetElement = _conversionService.Convert(sourceElement, sourceElement.GetType(), targetElementType); list.Add(targetElement); } else { list.Add(sourceElement); } } } return(list); }
public override object Convert(object source, Type sourceType, Type targetType) { if (source == null) { return(null); } var elementDesc = ConversionUtils.GetElementType(targetType); var target = ConversionUtils.CreateCompatListFor(targetType); if (elementDesc == null) { target.Add(source); } else { var singleElement = _conversionService.Convert(source, sourceType, elementDesc); target.Add(singleElement); } return(target); }
public override object Convert(object source, Type sourceType, Type targetType) { var asArray = source as Array; if (asArray == null) { return(null); } var len = asArray.GetLength(0); var arrayElementType = ConversionUtils.GetElementType(asArray.GetType()); var list = ConversionUtils.CreateCompatListFor(targetType); if (list == null) { throw new InvalidOperationException("Unable to create compatible list"); } if (!targetType.IsGenericType) { for (var i = 0; i < len; i++) { list.Add(asArray.GetValue(i)); } } else { var targetElementType = ConversionUtils.GetElementType(targetType); for (var i = 0; i < len; i++) { var sourceElement = asArray.GetValue(i); var targetElement = _conversionService.Convert(sourceElement, arrayElementType, targetElementType); list.Add(targetElement); } } return(list); }