Ejemplo 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();
 }
Ejemplo n.º 2
0
        public void PropertiesTest()
        {
            var converter = new Int16Converter();

            Assert.AreEqual(true, converter.AcceptsNativeType);
            Assert.AreEqual(typeof(short), converter.ConvertedType);
        }
Ejemplo 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&lt;int&gt;(value);
        /// </example>
        /// </summary>
        public static T ConvertTo <T>(this Int16Converter typeconverter, Object value)
        {
            if (typeconverter == null)
            {
                throw new ArgumentNullException("typeconverter");
            }

            return((T)typeconverter.ConvertTo(value, typeof(T)));
        }
Ejemplo 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&lt;int&gt;(context, culture, value);
        /// </example>
        /// </summary>
        public static T ConvertTo <T>(this Int16Converter 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)));
        }
Ejemplo n.º 5
0
        public void TargetType()
        {
            // Arrange
            IConverter converter    = new Int16Converter();
            var        expectedType = typeof(Int16);

            // Act
            var actualType = converter.TargetType;

            // Assert
            Assert.Equal(expectedType, actualType);
        }
Ejemplo 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);
        }
Ejemplo n.º 7
0
        public void Conversion()
        {
            // Arrange
            IConverter converter     = new Int16Converter();
            var        value         = "42";
            Int16      expectedValue = 42;

            // Act
            var actualValue = converter.Convert(value, converter.TargetType);

            // Assert
            Assert.NotNull(actualValue);
            Assert.IsType <Int16>(actualValue);
            Assert.Equal(expectedValue, (Int16)actualValue);
        }
        /// <summary>
        /// Tries to parse a 16-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 short result)
        {
            bool canConvert = true;

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

            return(canConvert);
        }
Ejemplo n.º 9
0
        public override void Encode(ushort obj, PacketData data)
        {
            Int16Converter conv  = obj;
            var            pos   = data.Position;
            var            bytes = data.Data;

            if (BitConverter.IsLittleEndian)
            {
                bytes[pos]     = conv.Byte0;
                bytes[pos + 1] = conv.Byte1;
            }
            else
            {
                bytes[pos + 1] = conv.Byte0;
                bytes[pos]     = conv.Byte1;
            }

            data.ShiftPosition(ByteSize);
        }
Ejemplo n.º 10
0
        public void BadValueConversion()
        {
            // Arrange
            IConverter converter = new Int16Converter();
            var        value     = "Hello";
            var        expectedExceptionMessage      = Constants.ExceptionMessages.FormatConverterUnableConvert(value, typeof(short));
            var        expectedInnerExceptionMessage = "Input string was not in a correct format.";

            // Act
            using (new LangageSwitcher("en-us"))
            {
                var actualException = Assert.Throws <CommandLineParserException>(() => converter.Convert(value, converter.TargetType));

                // Assert
                Assert.Equal(expectedExceptionMessage, actualException.Message);
                Assert.NotNull(actualException.InnerException);
                var actualInnerExecption = Assert.IsAssignableFrom <FormatException>(actualException.InnerException);
                Assert.Equal(expectedInnerExceptionMessage, actualInnerExecption.Message);
            }
        }
Ejemplo n.º 11
0
        public void ConvertFromExcelTest()
        {
            var converter            = new Int16Converter();
            var typeConverterOptions = new TypeConverterOptions {
                CultureInfo = CultureInfo.CurrentCulture
            };

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

            typeConverterOptions.NumberStyle = NumberStyles.HexNumber;
            Assert.AreEqual((short)0x123, converter.ConvertFromExcel(typeConverterOptions, "123"));

            try {
                converter.ConvertFromExcel(typeConverterOptions, "");
                Assert.Fail();
            } catch (ExcelTypeConverterException) {
            }
        }
Ejemplo n.º 12
0
 public void SetUp()
 {
     converter = new Int16Converter();
 }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 14
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);
        }