/// <exclude /> internal static object TryConvert(object value, Type targetType, out Exception conversionError) { conversionError = null; if (value == null) { if (!targetType.IsPrimitive) { return(null); } throw new InvalidOperationException(string.Format("Can not convert null to type '{0}'", targetType)); } if (targetType.IsInstanceOfType(value)) { return(value); } if (targetType.IsLazyGenericType() && !value.GetType().IsLazyGenericType()) { Type genericArgument = targetType.GetGenericArguments()[0]; object convertedValue = TryConvert(value, genericArgument, out conversionError); return(CreateLazyObject(() => convertedValue, genericArgument)); } var helper = targetType.GetCustomAttributesRecursively <ValueTypeConverterHelperAttribute>().FirstOrDefault(); if (helper != null) { object ret; if (helper.TryConvert(value, targetType, out ret)) { return(ret); } } var helper2 = value.GetType().GetCustomAttributesRecursively <ValueTypeConverterHelperAttribute>().FirstOrDefault(); if (helper2 != null) { object ret; if (helper2.TryConvert(value, targetType, out ret)) { return(ret); } } if (targetType == typeof(object)) { return(value); } if ((IsGenericEnumerable(value.GetType())) && (IsGenericEnumerable(targetType))) { Type targetItemType = targetType.GetGenericArguments()[0]; IList targetValue = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(new [] { targetItemType })); foreach (object valueItem in ((IEnumerable)value)) { object convertedValueItem = Convert(valueItem, targetItemType); targetValue.Add(convertedValueItem); } return(targetValue); } // Haz item, wantz list of it. if (!IsGenericEnumerable(value.GetType()) && IsGenericEnumerable(targetType)) { Type targetItemType = targetType.GetGenericArguments()[0]; if (targetItemType.IsInstanceOfType(value)) { IList targetValue = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(new [] { targetItemType })); if (value is string) { ((string)value).Split(',').ForEach(f => targetValue.Add(f)); } else { targetValue.Add(value); } return(targetValue); } } var stringValue = value as string; if (stringValue != null) { return(TryConvertStringValue(stringValue, targetType, ref conversionError)); } if (targetType == typeof(string)) { if (value is Type) { return(TypeManager.SerializeType((Type)value)); } if (value is DateTime) { return(XmlConvert.ToString((DateTime)value, XmlDateTimeSerializationMode.Local)); } if (value is IEnumerable) { return(string.Join(",", ((IEnumerable)value).Cast <string>().ToArray())); } } TypeConverter targetConverter = TypeDescriptor.GetConverter(targetType); if (targetConverter.CanConvertFrom(value.GetType())) { return(targetConverter.ConvertFrom(null, UserSettings.CultureInfo, value)); } TypeConverter valueConverter = TypeDescriptor.GetConverter(value.GetType()); if (valueConverter.CanConvertTo(targetType)) { return(valueConverter.ConvertTo(null, UserSettings.CultureInfo, value, targetType)); } throw new InvalidOperationException(string.Format("No conversion from {0} to {1} could be found", value.GetType(), targetType)); }