Esempio n. 1
0
        /// <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>
        /// imageformatconverter.ConvertTo<int>(context, culture, value);
        /// </example>
        /// </summary>
        public static T ConvertTo <T>(this ImageFormatConverter imageformatconverter, System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, Object value)
        {
            if (imageformatconverter == null)
            {
                throw new ArgumentNullException("imageformatconverter");
            }

            return((T)imageformatconverter.ConvertTo(context, culture, value, typeof(T)));
        }
Esempio n. 2
0
        /// <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 ImageFormatConverter typeconverter, Object value)
        {
            if (typeconverter == null)
            {
                throw new ArgumentNullException("typeconverter");
            }

            return((T)typeconverter.ConvertTo(value, typeof(T)));
        }
        public void TestConvertTo_String()
        {
            Assert.Equal(_imageFmtStr, (string)_imgFmtConv.ConvertTo(null, CultureInfo.InvariantCulture, _imageFmt, typeof(string)));
            Assert.Equal(_imageFmtStr, (string)_imgFmtConv.ConvertTo(_imageFmt, typeof(string)));

            Assert.Equal(_imageFmtStr, (string)_imgFmtConvFrmTD.ConvertTo(null, CultureInfo.InvariantCulture, _imageFmt, typeof(string)));
            Assert.Equal(_imageFmtStr, (string)_imgFmtConvFrmTD.ConvertTo(_imageFmt, typeof(string)));

            Assert.Equal(string.Empty, (string)_imgFmtConv.ConvertTo(null, typeof(string)));
            Assert.Equal(string.Empty, (string)_imgFmtConv.ConvertTo(null, CultureInfo.CreateSpecificCulture("ru-RU"), null, typeof(string)));

            Assert.Equal(string.Empty, (string)_imgFmtConvFrmTD.ConvertTo(null, typeof(string)));
            Assert.Equal(string.Empty, (string)_imgFmtConvFrmTD.ConvertTo(null, CultureInfo.CreateSpecificCulture("de-DE"), null, typeof(string)));
        }
Esempio n. 4
0
        public void TestConvertTo_String()
        {
            Assert.Equal(_imageFmtStr, (string)_imgFmtConv.ConvertTo(null, CultureInfo.InvariantCulture, _imageFmt, typeof(string)));
            Assert.Equal(_imageFmtStr, (string)_imgFmtConv.ConvertTo(_imageFmt, typeof(string)));

            Assert.Equal(_imageFmtStr, (string)_imgFmtConvFrmTD.ConvertTo(null, CultureInfo.InvariantCulture, _imageFmt, typeof(string)));
            Assert.Equal(_imageFmtStr, (string)_imgFmtConvFrmTD.ConvertTo(_imageFmt, typeof(string)));
        }
Esempio n. 5
0
        public void TestConvertTo()
        {
            Assert.AreEqual(imageFmtStr, (String)imgFmtConv.ConvertTo(null,
                                                                      CultureInfo.InvariantCulture,
                                                                      imageFmt, typeof(String)), "CT#1");

            Assert.AreEqual(imageFmtStr, (String)imgFmtConv.ConvertTo(imageFmt,
                                                                      typeof(String)), "CT#1a");

            try {
                imgFmtConv.ConvertTo(null, CultureInfo.InvariantCulture,
                                     imageFmt, typeof(ImageFormat));
                Assert.Fail("CT#2: must throw NotSupportedException");
            } catch (Exception e) {
                Assert.IsTrue(e is NotSupportedException, "CT#2");
            }

            try {
                imgFmtConv.ConvertTo(null, CultureInfo.InvariantCulture,
                                     imageFmt, typeof(Guid));
                Assert.Fail("CT#2a: must throw NotSupportedException");
            } catch (Exception e) {
                Assert.IsTrue(e is NotSupportedException, "CT#2a");
            }

            try {
                imgFmtConv.ConvertTo(null, CultureInfo.InvariantCulture,
                                     imageFmt, typeof(Object));
                Assert.Fail("CT#3: must throw NotSupportedException");
            } catch (Exception e) {
                Assert.IsTrue(e is NotSupportedException, "CT#3");
            }

            try {
                imgFmtConv.ConvertTo(null, CultureInfo.InvariantCulture,
                                     imageFmt, typeof(int));
                Assert.Fail("CT#4: must throw NotSupportedException");
            } catch (Exception e) {
                Assert.IsTrue(e is NotSupportedException, "CT#4");
            }


            Assert.AreEqual(imageFmtStr, (String)imgFmtConvFrmTD.ConvertTo(null,
                                                                           CultureInfo.InvariantCulture,
                                                                           imageFmt, typeof(String)), "CT#1A");

            Assert.AreEqual(imageFmtStr, (String)imgFmtConvFrmTD.ConvertTo(imageFmt,
                                                                           typeof(String)), "CT#1aA");

            try {
                imgFmtConvFrmTD.ConvertTo(null, CultureInfo.InvariantCulture,
                                          imageFmt, typeof(ImageFormat));
                Assert.Fail("CT#2A: must throw NotSupportedException");
            } catch (Exception e) {
                Assert.IsTrue(e is NotSupportedException, "CT#2A");
            }

            try {
                imgFmtConvFrmTD.ConvertTo(null, CultureInfo.InvariantCulture,
                                          imageFmt, typeof(Guid));
                Assert.Fail("CT#2aA: must throw NotSupportedException");
            } catch (Exception e) {
                Assert.IsTrue(e is NotSupportedException, "CT#2aA");
            }

            try {
                imgFmtConvFrmTD.ConvertTo(null, CultureInfo.InvariantCulture,
                                          imageFmt, typeof(Object));
                Assert.Fail("CT#3A: must throw NotSupportedException");
            } catch (Exception e) {
                Assert.IsTrue(e is NotSupportedException, "CT#3A");
            }

            try {
                imgFmtConvFrmTD.ConvertTo(null, CultureInfo.InvariantCulture,
                                          imageFmt, typeof(int));
                Assert.Fail("CT#4A: must throw NotSupportedException");
            } catch (Exception e) {
                Assert.IsTrue(e is NotSupportedException, "CT#4A");
            }
        }