Exemplo n.º 1
0
 static Converter()
 {
     BoolConverter.Initialize();
     CharConverter.Initialize();
     ByteConverter.Initialize();
     SByteConverter.Initialize();
     Int16Converter.Initialize();
     UInt16Converter.Initialize();
     Int32Converter.Initialize();
     UInt32Converter.Initialize();
     Int64Converter.Initialize();
     UInt64Converter.Initialize();
     SingleConverter.Initialize();
     DoubleConverter.Initialize();
     DecimalConverter.Initialize();
     BigIntegerConverter.Initialize();
     BytesConverter.Initialize();
     CharsConverter.Initialize();
     StringConverter.Initialize();
     StringBuilderConverter.Initialize();
     DateTimeConverter.Initialize();
     TimeSpanConverter.Initialize();
     GuidConverter.Initialize();
     MemoryStreamConverter.Initialize();
     StreamConverter.Initialize();
 }
Exemplo n.º 2
0
        public void PropertiesTest()
        {
            var converter = new SByteConverter();

            Assert.AreEqual(true, converter.AcceptsNativeType);
            Assert.AreEqual(typeof(sbyte), converter.ConvertedType);
        }
Exemplo n.º 3
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 SByteConverter typeconverter, Object value)
        {
            if (typeconverter == null)
            {
                throw new ArgumentNullException("typeconverter");
            }

            return((T)typeconverter.ConvertTo(value, typeof(T)));
        }
Exemplo n.º 4
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>
        /// basenumberconverter.ConvertTo<int>(context, culture, value);
        /// </example>
        /// </summary>
        public static T ConvertTo <T>(this SByteConverter basenumberconverter, ITypeDescriptorContext context, System.Globalization.CultureInfo culture, Object value)
        {
            if (basenumberconverter == null)
            {
                throw new ArgumentNullException("basenumberconverter");
            }

            return((T)basenumberconverter.ConvertTo(context, culture, value, typeof(T)));
        }
        public void CanSerializeAndDeserialize()
        {
            sbyte value = sbyte.MaxValue;
            SByteConverter converter = new SByteConverter();
            byte[] bytes = converter.Serialize(value);

            sbyte valueFromBytes = converter.Deserialize(bytes);

            Assert.Equal(valueFromBytes, value);
        }
Exemplo n.º 6
0
        private JsonFxAOT()
        {
            TypeConverter c;

            c          = new ArrayConverter();
            m_fakeFlag = c.Equals(c);
            //c = new BaseNumberConverter();
            //m_fakeFlag = c.Equals(c);
            c          = new BooleanConverter();
            m_fakeFlag = c.Equals(c);
            c          = new ByteConverter();
            m_fakeFlag = c.Equals(c);
            c          = new CollectionConverter();
            m_fakeFlag = c.Equals(c);
            c          = new ComponentConverter(typeof(int));
            m_fakeFlag = c.Equals(c);
            c          = new CultureInfoConverter();
            m_fakeFlag = c.Equals(c);
            c          = new DateTimeConverter();
            m_fakeFlag = c.Equals(c);
            c          = new DecimalConverter();
            m_fakeFlag = c.Equals(c);
            c          = new DoubleConverter();
            m_fakeFlag = c.Equals(c);
            c          = new EnumConverter(typeof(int));
            m_fakeFlag = c.Equals(c);
            c          = new ExpandableObjectConverter();
            m_fakeFlag = c.Equals(c);
            c          = new Int16Converter();
            m_fakeFlag = c.Equals(c);
            c          = new Int32Converter();
            m_fakeFlag = c.Equals(c);
            c          = new Int64Converter();
            m_fakeFlag = c.Equals(c);
            c          = new NullableConverter(typeof(object));
            m_fakeFlag = c.Equals(c);
            c          = new SByteConverter();
            m_fakeFlag = c.Equals(c);
            c          = new SingleConverter();
            m_fakeFlag = c.Equals(c);
            c          = new StringConverter();
            m_fakeFlag = c.Equals(c);
            c          = new TimeSpanConverter();
            m_fakeFlag = c.Equals(c);
            c          = new UInt16Converter();
            m_fakeFlag = c.Equals(c);
            c          = new UInt32Converter();
            m_fakeFlag = c.Equals(c);
            c          = new UInt64Converter();
            m_fakeFlag = c.Equals(c);
        }
        /// <summary>
        /// Tries to parse a 8-bit integer from a string. This method should handle hexadecimal values
        /// as well as normal values.
        /// </summary>
        /// <param name="value">The string value to parse.</param>
        /// <param name="result">The parsed integer, if the string was valid. If invalid, this
        /// will be the default integer value.</param>
        /// <returns>True if the conversion was successful; otherwise returns false.</returns>
        public static bool TryParseEx(string value, out sbyte result)
        {
            bool canConvert = true;

            try
            {
                var converter = new SByteConverter();
                result = (sbyte)converter.ConvertFromString(value);
            }
            catch (Exception)
            {
                result     = default(sbyte);
                canConvert = false;
            }

            return(canConvert);
        }
Exemplo n.º 8
0
        public void ConvertFromExcelTest()
        {
            var converter            = new SByteConverter();
            var typeConverterOptions = new TypeConverterOptions {
                CultureInfo = CultureInfo.CurrentCulture
            };

            Assert.AreEqual((sbyte)123, converter.ConvertFromExcel(typeConverterOptions, (double)123));
            Assert.AreEqual((sbyte)123, converter.ConvertFromExcel(typeConverterOptions, "123"));
            Assert.AreEqual((sbyte)123, converter.ConvertFromExcel(typeConverterOptions, " 123 "));
            Assert.AreEqual((sbyte)0, converter.ConvertFromExcel(typeConverterOptions, null));

            typeConverterOptions.NumberStyle = NumberStyles.HexNumber;
            Assert.AreEqual((sbyte)0x12, converter.ConvertFromExcel(typeConverterOptions, "12"));

            try {
                converter.ConvertFromExcel(typeConverterOptions, "");
                Assert.Fail();
            } catch (ExcelTypeConverterException) {
            }
        }
Exemplo n.º 9
0
 public void SetUp()
 {
     converter = new SByteConverter();
 }
Exemplo n.º 10
0
        internal static TypeConverter GetCoreConverterFromCustomType(Type type)
        {
            TypeConverter result = null;

            if (type.IsEnum)
            {
                result = new EnumConverter(type);
            }
            else if (typeof(int).IsAssignableFrom(type))
            {
                result = new Int32Converter();
            }
            else if (typeof(short).IsAssignableFrom(type))
            {
                result = new Int16Converter();
            }
            else if (typeof(long).IsAssignableFrom(type))
            {
                result = new Int64Converter();
            }
            else if (typeof(uint).IsAssignableFrom(type))
            {
                result = new UInt32Converter();
            }
            else if (typeof(ushort).IsAssignableFrom(type))
            {
                result = new UInt16Converter();
            }
            else if (typeof(ulong).IsAssignableFrom(type))
            {
                result = new UInt64Converter();
            }
            else if (typeof(bool).IsAssignableFrom(type))
            {
                result = new BooleanConverter();
            }
            else if (typeof(double).IsAssignableFrom(type))
            {
                result = new DoubleConverter();
            }
            else if (typeof(float).IsAssignableFrom(type))
            {
                result = new SingleConverter();
            }
            else if (typeof(byte).IsAssignableFrom(type))
            {
                result = new ByteConverter();
            }
            else if (typeof(sbyte).IsAssignableFrom(type))
            {
                result = new SByteConverter();
            }
            else if (typeof(char).IsAssignableFrom(type))
            {
                result = new CharConverter();
            }
            else if (typeof(decimal).IsAssignableFrom(type))
            {
                result = new DecimalConverter();
            }
            else if (typeof(TimeSpan).IsAssignableFrom(type))
            {
                result = new TimeSpanConverter();
            }
            else if (typeof(Guid).IsAssignableFrom(type))
            {
                result = new GuidConverter();
            }
            else if (typeof(string).IsAssignableFrom(type))
            {
                result = new StringConverter();
            }
            else if (typeof(CultureInfo).IsAssignableFrom(type))
            {
                result = new CultureInfoConverter();
            }
            else if (typeof(Type).IsAssignableFrom(type))
            {
                result = new TypeTypeConverter();
            }
            else if (typeof(DateTime).IsAssignableFrom(type))
            {
                result = new DateTimeConverter2();
            }
            return(result);
        }
Exemplo n.º 11
0
        private static TypeConverter GetCoreConverterFromCoreType(Type type)
        {
            TypeConverter result = null;

            if (type == typeof(int))
            {
                result = new Int32Converter();
            }
            else if (type == typeof(short))
            {
                result = new Int16Converter();
            }
            else if (type == typeof(long))
            {
                result = new Int64Converter();
            }
            else if (type == typeof(uint))
            {
                result = new UInt32Converter();
            }
            else if (type == typeof(ushort))
            {
                result = new UInt16Converter();
            }
            else if (type == typeof(ulong))
            {
                result = new UInt64Converter();
            }
            else if (type == typeof(bool))
            {
                result = new BooleanConverter();
            }
            else if (type == typeof(double))
            {
                result = new DoubleConverter();
            }
            else if (type == typeof(float))
            {
                result = new SingleConverter();
            }
            else if (type == typeof(byte))
            {
                result = new ByteConverter();
            }
            else if (type == typeof(sbyte))
            {
                result = new SByteConverter();
            }
            else if (type == typeof(char))
            {
                result = new CharConverter();
            }
            else if (type == typeof(decimal))
            {
                result = new DecimalConverter();
            }
            else if (type == typeof(TimeSpan))
            {
                result = new TimeSpanConverter();
            }
            else if (type == typeof(Guid))
            {
                result = new GuidConverter();
            }
            else if (type == typeof(string))
            {
                result = new StringConverter();
            }
            else if (type == typeof(CultureInfo))
            {
                result = new CultureInfoConverter();
            }
            else if (type == typeof(Type))
            {
                result = new TypeTypeConverter();
            }
            else if (type == typeof(DateTime))
            {
                result = new DateTimeConverter2();
            }
            else if (ReflectionHelper.IsNullableType(type))
            {
                result = new NullableConverter(type);
            }
            return(result);
        }