ConvertFrom() public method

Converts a String or Int32 to an Encoding.
Returns Encoding.Default if the value is null or an empty string.
No could be matched to the string or code page integer.
public ConvertFrom ( ITypeDescriptorContext context, CultureInfo culture, object value ) : object
context ITypeDescriptorContext Additional context for conversion.
culture System.Globalization.CultureInfo The culture to use for conversion.
value object The or value to convert.
return object
Example #1
0
        public void ConvertFromStringPropertyName()
        {
            var converter = new EncodingConverter();
            Assert.IsTrue(converter.CanConvertFrom(typeof(string)), "Cannot convert from a string.");

            var value = (Encoding)converter.ConvertFrom("UTF8");
            Assert.AreEqual<Encoding>(Encoding.UTF8, value, "Could not convert from a string.");
        }
Example #2
0
        public void ConvertFromNull()
        {
            var converter = new EncodingConverter();
            Assert.IsTrue(converter.CanConvertFrom(typeof(string)), "Cannot convert from a string.");

            var value = (Encoding)converter.ConvertFrom(null);
            Assert.AreEqual<Encoding>(Encoding.Default, value, "Could not convert from a null string.");
        }
Example #3
0
        public void ConvertFromIntegerCodePage()
        {
            var converter = new EncodingConverter();
            Assert.IsTrue(converter.CanConvertFrom(typeof(int)), "Cannot convert from an integer.");

            var value = (Encoding)converter.ConvertFrom(65001);
            Assert.AreEqual<Encoding>(Encoding.UTF8, value, "Could not convert from an integer.");
        }
Example #4
0
        public void ConvertFromIntegerCodePage()
        {
            var converter = new EncodingConverter();

            Assert.IsTrue(converter.CanConvertFrom(typeof(int)), "Cannot convert from an integer.");

            var value = (Encoding)converter.ConvertFrom(65001);

            Assert.AreEqual <Encoding>(Encoding.UTF8, value, "Could not convert from an integer.");
        }
Example #5
0
        public void ConvertFromNull()
        {
            var converter = new EncodingConverter();

            Assert.IsTrue(converter.CanConvertFrom(typeof(string)), "Cannot convert from a string.");

            var value = (Encoding)converter.ConvertFrom(null);

            Assert.AreEqual <Encoding>(Encoding.Default, value, "Could not convert from a null string.");
        }
Example #6
0
        public void ConvertFromStringEncodingName()
        {
            var converter = new EncodingConverter();

            Assert.IsTrue(converter.CanConvertFrom(typeof(string)), "Cannot convert from a string.");

            var value = (Encoding)converter.ConvertFrom("utf-8");

            Assert.AreEqual <Encoding>(Encoding.UTF8, value, "Could not convert from a string.");
        }