/// <summary> /// Extends ConvertTo so that methods that return a specific type object given a Type parameter can be /// used as generic method and casting is not required. /// <example> /// imageindexconverter.ConvertTo<int>(context, culture, value); /// </example> /// </summary> public static T ConvertTo <T>(this ImageIndexConverter imageindexconverter, System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, Object value) { if (imageindexconverter == null) { throw new ArgumentNullException("imageindexconverter"); } return((T)imageindexconverter.ConvertTo(context, culture, value, typeof(T))); }
/// <summary> /// Extends ConvertTo so that methods that return a specific type object given a Type parameter can be /// used as generic method and casting is not required. /// <example> /// typeconverter.ConvertTo<int>(value); /// </example> /// </summary> public static T ConvertTo <T>(this ImageIndexConverter typeconverter, Object value) { if (typeconverter == null) { throw new ArgumentNullException("typeconverter"); } return((T)typeconverter.ConvertTo(value, typeof(T))); }
public void TestConvertTo() { Assert.AreEqual("(none)", ic.ConvertTo(null, CultureInfo.InvariantCulture, -1, typeof(string)), "ConvertInt_Minus1_ToStr"); Assert.AreEqual("0", ic.ConvertTo(null, CultureInfo.InvariantCulture, 0, typeof(string)), "ConvertInt_0_ToStr"); Assert.AreEqual("1", ic.ConvertTo(null, CultureInfo.InvariantCulture, 1, typeof(string)), "ConvertInt_1_ToStr"); Assert.AreEqual(0, ic.ConvertTo(null, CultureInfo.InvariantCulture, 0, typeof(int)), "ConvertInt_0_ToInt"); Assert.AreEqual("(none)", ic.ConvertTo(null, CultureInfo.InvariantCulture, "(none)", typeof(string)), "ConvertStr_none_ToStr"); Assert.AreEqual("-1", ic.ConvertTo(null, CultureInfo.InvariantCulture, "-1", typeof(string)), "ConvertStr_Minus1_ToStr"); Assert.AreEqual(-1, ic.ConvertTo(null, CultureInfo.InvariantCulture, -1, typeof(int)), "ConvertInt_Minus1_ToInt"); Assert.AreEqual(1, ic.ConvertTo(null, CultureInfo.InvariantCulture, 1, typeof(int)), "ConvertInt_1_ToInt"); Assert.AreEqual(-1, ic.ConvertTo(null, CultureInfo.InvariantCulture, "-1", typeof(int)), "ConvertStr_Minus1_ToInt"); Assert.AreEqual(0, ic.ConvertTo(null, CultureInfo.InvariantCulture, "0", typeof(int)), "ConvertStr_0_ToInt"); Assert.AreEqual(1, ic.ConvertTo(null, CultureInfo.InvariantCulture, "1", typeof(int)), "ConvertStr_1_ToInt"); Assert.AreEqual(2, ic.ConvertTo(null, CultureInfo.InvariantCulture, 1.5f, typeof(int)), "ConvertFloat_1_5_ToInt must return 2."); try { ic.ConvertTo(null, CultureInfo.InvariantCulture, "-1.5f", typeof(int)); Assert.Fail("ConvertFloatStrToInt must throw exception."); } catch (Exception e) { Assert.IsTrue(e is FormatException, "ConvertFloatStrToInt must throw FormatException."); } Assert.AreEqual(1.5, ic.ConvertTo(null, CultureInfo.InvariantCulture, 1.5f, typeof(float)), "ConvertFloat_1_5_ToFloat"); }