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) { if (source == null) { return(null); } var targetElementType = ConversionUtils.GetElementType(targetType); if (targetElementType == null) { throw new InvalidOperationException("No target element type"); } var enumerable = source as IEnumerable; var len = ConversionUtils.Count(enumerable); var array = Array.CreateInstance(targetElementType, len); var i = 0; foreach (var sourceElement in enumerable) { var targetElement = _conversionService.Convert(sourceElement, sourceElement.GetType(), targetElementType); array.SetValue(targetElement, i++); } return(array); }
public override bool Matches(Type sourceType, Type targetType) { if (sourceType == typeof(string) && ConversionUtils.CanCreateCompatListFor(targetType)) { return(ConversionUtils.CanConvertElements(sourceType, ConversionUtils.GetElementType(targetType), _conversionService)); } return(false); }
public override bool Matches(Type sourceType, Type targetType) { if (!targetType.IsArray || sourceType.IsArray) { return(false); } return(ConversionUtils.CanConvertElements( ConversionUtils.GetElementType(sourceType), ConversionUtils.GetElementType(targetType), _conversionService)); }
public override bool Matches(Type sourceType, Type targetType) { // NO OP check Arrays already implement IList, etc. if (targetType.IsAssignableFrom(sourceType)) { return(false); } if (sourceType.IsArray && ConversionUtils.CanCreateCompatListFor(targetType)) { return(ConversionUtils.CanConvertElements( ConversionUtils.GetElementType(sourceType), ConversionUtils.GetElementType(targetType), _conversionService)); } return(false); }
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); }