/// <summary> /// Performs several special conversions allowed by NUnit in order to /// permit arguments with types that cannot be used in the constructor /// of an Attribute such as TestCaseAttribute or to simplify their use. /// </summary> /// <param name="value">The value to be converted</param> /// <param name="targetType">The target <see cref="Type"/> in which the <paramref name="value"/> should be converted</param> /// <param name="convertedValue">If conversion was successfully applied, the <paramref name="value"/> converted into <paramref name="targetType"/></param> /// <returns> /// <c>true</c> if <paramref name="value"/> was converted and <paramref name="convertedValue"/> should be used; /// <c>false</c> is no conversion was applied and <paramref name="convertedValue"/> should be ignored /// </returns> public static bool TryConvert(object value, Type targetType, out object convertedValue) { if (targetType.IsInstanceOfType(value)) { convertedValue = value; return(true); } if (value == null || value.GetType().FullName == "System.DBNull") { convertedValue = null; return(Reflect.IsAssignableFromNull(targetType)); } bool convert = false; var underlyingTargetType = Nullable.GetUnderlyingType(targetType) ?? targetType; if (underlyingTargetType == typeof(short) || underlyingTargetType == typeof(byte) || underlyingTargetType == typeof(sbyte) || underlyingTargetType == typeof(long) || underlyingTargetType == typeof(double)) { convert = value is int; } else if (underlyingTargetType == typeof(decimal)) { convert = value is double || value is string || value is int; } else if (underlyingTargetType == typeof(DateTime)) { convert = value is string; } if (convert) { convertedValue = System.Convert.ChangeType(value, underlyingTargetType, CultureInfo.InvariantCulture); return(true); } var converter = TypeDescriptor.GetConverter(underlyingTargetType); if (converter.CanConvertFrom(value.GetType())) { convertedValue = converter.ConvertFrom(null, CultureInfo.InvariantCulture, value); return(true); } convertedValue = null; return(false); }
/// <summary> /// Converts a single value to the <paramref name="targetType"/>, if it is supported. /// </summary> public static bool TryConvert(object value, Type targetType, out object convertedValue) { if (targetType.IsInstanceOfType(value)) { convertedValue = value; return(true); } if (value == null || value.GetType().FullName == "System.DBNull") { convertedValue = null; return(Reflect.IsAssignableFromNull(targetType)); } bool convert = false; if (targetType == typeof(short) || targetType == typeof(byte) || targetType == typeof(sbyte)) { convert = value is int; } else if (targetType == typeof(decimal)) { convert = value is double || value is string || value is int; } else if (targetType == typeof(DateTime) || targetType == typeof(TimeSpan)) { convert = value is string; } if (convert) { convertedValue = System.Convert.ChangeType(value, targetType, System.Globalization.CultureInfo.InvariantCulture); return(true); } convertedValue = null; return(false); }