diff --git a/X10D.Tests/src/Core/ConvertibleTests.cs b/X10D.Tests/src/Core/ConvertibleTests.cs deleted file mode 100644 index cc11fc2..0000000 --- a/X10D.Tests/src/Core/ConvertibleTests.cs +++ /dev/null @@ -1,58 +0,0 @@ -namespace X10D.Tests.Core -{ - using System; - using Microsoft.VisualStudio.TestTools.UnitTesting; - - /// - /// Tests for . - /// - [TestClass] - public class ConvertibleTests - { - /// - /// Tests for . - /// - [TestMethod] - public void To() - { - Assert.AreEqual(2, "2".To()); - Assert.AreEqual("12.5", 12.50.To()); - Assert.IsTrue("True".To()); - } - - /// - /// Tests for . - /// - [TestMethod] - public void ToOrDefault() - { - Assert.AreEqual(2, "2".ToOrDefault()); - Assert.AreEqual("12.5", 12.50.ToOrDefault()); - Assert.IsTrue("True".ToOrDefault()); - Assert.ThrowsException(() => "Foo".ToOrDefault()); - Assert.IsTrue("1.5".ToOrDefault(out float f)); - Assert.AreEqual(1.5f, f); - } - - /// - /// Tests for . - /// - [TestMethod] - public void ToOrNull() - { - Assert.IsFalse("foo".ToOrNull(out ConvertibleTests t)); - Assert.IsNull(t); - } - - /// - /// Tests for . - /// - [TestMethod] - public void ToOrOther() - { - Assert.AreEqual(2.0, "Foo".ToOrOther(2.0)); - Assert.IsFalse("Foo".ToOrOther(out var d, 2.0)); - Assert.AreEqual(2.0, d); - } - } -} diff --git a/X10D/src/ConvertibleExtensions.cs b/X10D/src/ConvertibleExtensions.cs deleted file mode 100644 index bf2a3ce..0000000 --- a/X10D/src/ConvertibleExtensions.cs +++ /dev/null @@ -1,161 +0,0 @@ -namespace X10D -{ - using System; - - /// - /// Extension methods for . - /// - public static class ConvertibleExtensions - { - /// - /// Converts the object to another type. - /// - /// The type to convert to. - /// The object to convert. - /// An object that supplies culture-specific formatting information. - /// Returns the value converted to . - /// - /// This conversion is not supported. - /// -or- - /// is and is a value type. - /// - [CLSCompliant(false)] - public static T To(this IConvertible value, IFormatProvider provider = null) - { - if (value is null) - { - return default; - } - - return (T)Convert.ChangeType(value, typeof(T), provider); - } - - /// - /// Converts the object to another type, returning the default value on failure. - /// - /// The type to convert to. - /// The object to convert. - /// The format provider. - /// Returns the value converted to . - /// This conversion is not supported. - [CLSCompliant(false)] - public static T ToOrDefault(this IConvertible value, IFormatProvider provider = null) - { - return value is null ? default : To(value, provider); - } - - /// - /// Converts the object to another type, returning the default value on failure. - /// - /// The type to convert to. - /// The object to convert. - /// The parameter where the result should be sent. - /// An object that supplies culture-specific formatting information. - /// Returns on success, on failure. - [CLSCompliant(false)] - public static bool ToOrDefault(this IConvertible value, out T newObj, IFormatProvider provider = null) - { - if (value is null) - { - newObj = default; - return false; - } - - try - { - newObj = To(value, provider); - return true; - } - catch (InvalidCastException) - { - newObj = default; - return false; - } - } - - /// - /// Converts the object to another type, returning on failure. - /// - /// The type to convert to. - /// The object to convert. - /// An object that supplies culture-specific formatting information. - /// Returns a or . - [CLSCompliant(false)] - public static T ToOrNull(this IConvertible value, IFormatProvider provider = null) - where T : class - { - return value.ToOrNull(out T v, provider) ? v : null; - } - - /// - /// Converts the object to another type, returning on failure. - /// - /// The type to convert to. - /// The object to convert. - /// The parameter where the result should be sent. - /// An object that supplies culture-specific formatting information. - /// Returns a or . - [CLSCompliant(false)] - public static bool ToOrNull(this IConvertible value, out T newObj, IFormatProvider provider = null) - where T : class - { - return ToOrOther(value, out newObj, null, provider); - } - - /// - /// Converts the object to another type, returning a different value on failure. - /// - /// The type to convert to. - /// The object to convert. - /// The backup value. - /// An object that supplies culture-specific formatting information. - /// Returns the value converted to . - [CLSCompliant(false)] - public static T ToOrOther(this IConvertible value, T other, IFormatProvider provider = null) - { - if (value is null) - { - return other; - } - - try - { - return To(value, provider); - } - catch (Exception ex) when (ex is InvalidCastException || ex is FormatException) - { - return other; - } - } - - /// - /// Converts the object to another type, returning a different value on failure. - /// - /// The type to convert to. - /// The object to convert. - /// The parameter where the result should be sent. - /// The backup value. - /// An object that supplies culture-specific formatting information. - /// Returns on success, on failure. - [CLSCompliant(false)] - public static bool ToOrOther(this IConvertible value, out T newObj, T other, IFormatProvider provider = null) - { - if (value is null) - { - newObj = other; - return false; - } - - try - { - newObj = To(value, provider); - return true; - } - catch (Exception ex) when (ex is InvalidCastException || ex is FormatException) - { - newObj = other; - return false; - } - } - } -}