コード例 #1
0
        public void TestToLong()
        {
            DateTime date = new DateTime();

            Assert.Equal(date.Ticks, LongConverter.ToLong(date));
            Assert.Equal(100, LongConverter.ToLong(new TimeSpan(100)));
            Assert.Equal(123, LongConverter.ToLong(123));
            Assert.Equal(123, LongConverter.ToLong((short)123));
            Assert.Equal(123, LongConverter.ToLong(123.456));
            Assert.Equal(124, LongConverter.ToLong(123.999));
            Assert.Equal(123, LongConverter.ToLong(DoubleConverter.ToDouble(123.456)));
            Assert.Equal(123, LongConverter.ToLong("123"));
            Assert.Equal(123, LongConverter.ToLong("123.465"));
            Assert.Equal(123, LongConverter.ToLong("123.999"));
            Assert.Equal(0, LongConverter.ToLong(null));
        }
        /// <summary>
        /// Converts value into an object type specified by Type Code
        /// or returns default value when conversion is not possible.
        /// </summary>
        /// <typeparam name="T">the Class type for the data type into which 'value' is to be converted.</typeparam>
        /// <param name="value">the value to convert.</param>
        /// <param name="defaultValue">the default value to return if conversion is not possible
        /// (returns null).</param>
        /// <returns>object value of type corresponding to TypeCode, or default value when
        /// conversion is not supported</returns>
        public static T ToTypeWithDefault <T>(object value, T defaultValue)
        {
            if (value == null)
            {
                return(defaultValue);
            }
            if (value is T)
            {
                return((T)value);
            }

            var typeInfo = typeof(T).GetTypeInfo();

            if (typeInfo.IsEnum)
            {
                value = EnumConverter.ToEnumWithDefault <T>(value, defaultValue);
            }
            else if (typeInfo.IsAssignableFrom(typeof(decimal)))
            {
                value = DecimalConverter.ToNullableDecimal(value);
            }
            else if (typeInfo.IsAssignableFrom(typeof(string)))
            {
                value = StringConverter.ToNullableString(value);
            }
            else if (typeInfo.IsAssignableFrom(typeof(long)))
            {
                value = LongConverter.ToNullableLong(value);
            }
            else if (typeInfo.IsAssignableFrom(typeof(int)))
            {
                value = IntegerConverter.ToNullableInteger(value);
            }
            else if (typeInfo.IsAssignableFrom(typeof(double)))
            {
                value = DoubleConverter.ToNullableDouble(value);
            }
            else if (typeInfo.IsAssignableFrom(typeof(float)))
            {
                value = FloatConverter.ToNullableFloat(value);
            }
            else if (typeInfo.IsAssignableFrom(typeof(bool)))
            {
                value = BooleanConverter.ToNullableBoolean(value);
            }
            else if (typeInfo.IsAssignableFrom(typeof(DateTime)))
            {
                value = DateTimeConverter.ToNullableDateTime(value);
            }
            else if (typeInfo.IsAssignableFrom(typeof(TimeSpan)))
            {
                value = TimeSpanConverter.ToNullableTimeSpan(value);
            }

            if (value == null)
            {
                return(defaultValue);
            }

            try
            {
                if (typeInfo.IsClass || typeInfo.IsInterface)
                {
                    return(value is T ? (T)value : defaultValue);
                }
                else
                {
                    return((T)value);
                }
            }
            catch
            {
                return(defaultValue);
            }
        }
コード例 #3
0
 public void TestToLongWithDefault()
 {
     Assert.Equal(123, LongConverter.ToLongWithDefault(null, 123));
     Assert.Equal(0, LongConverter.ToLongWithDefault(false, 123));
     Assert.Equal(123, LongConverter.ToLongWithDefault("ABC", 123));
 }
        /// <summary>
        /// Converts value into an object type specified by Type Code or returns null
        /// when conversion is not possible.
        /// </summary>
        /// <typeparam name="T">the Class type for the data type.</typeparam>
        /// <param name="value">the value to convert.</param>
        /// <returns>object value of type corresponding to TypeCode, or null when
        /// conversion is not supported.</returns>
        /// See <see cref="TypeConverter.ToTypeCode(Type)"/>
        public static T?ToNullableType <T>(object value) where T : struct
        {
            if (value == null)
            {
                return(null);
            }
            if (value is T)
            {
                return((T)value);
            }

            var typeInfo = typeof(T).GetTypeInfo();

            if (typeInfo.IsEnum)
            {
                value = EnumConverter.ToNullableEnum <T>(value);
            }
            else if (typeInfo.IsAssignableFrom(typeof(decimal)))
            {
                value = DecimalConverter.ToNullableDecimal(value);
            }
            else if (typeInfo.IsAssignableFrom(typeof(string)))
            {
                value = StringConverter.ToNullableString(value);
            }
            else if (typeInfo.IsAssignableFrom(typeof(long)))
            {
                value = LongConverter.ToNullableLong(value);
            }
            else if (typeInfo.IsAssignableFrom(typeof(int)))
            {
                value = IntegerConverter.ToNullableInteger(value);
            }
            else if (typeInfo.IsAssignableFrom(typeof(double)))
            {
                value = DoubleConverter.ToNullableDouble(value);
            }
            else if (typeInfo.IsAssignableFrom(typeof(float)))
            {
                value = FloatConverter.ToNullableFloat(value);
            }
            else if (typeInfo.IsAssignableFrom(typeof(DateTime)))
            {
                value = DateTimeConverter.ToNullableDateTime(value);
            }
            else if (typeInfo.IsAssignableFrom(typeof(TimeSpan)))
            {
                value = TimeSpanConverter.ToNullableTimeSpan(value);
            }

            if (value == null)
            {
                return(null);
            }

            try
            {
                return((T)value);
            }
            catch
            {
                return(null);
            }
        }