Пример #1
0
        public virtual object ConvertToValue(string input, string typeName)
        {
            if (typeName == null)
            {
                return(input);
            }

            if (!int.TryParse(typeName, NumberStyles.Integer, CultureInfo.InvariantCulture, out var i))
            {
                var type = Type.GetType(typeName, true);
                if (type.IsEnum)
                {
                    return(Conversions.ToEnum(input, type));
                }

                return(Conversions.ChangeType(input, type, null, CultureInfo.InvariantCulture));
            }

            switch (i)
            {
            case (int)TypeCode.Boolean:
                return(bool.Parse(input));

            case (int)TypeCode.Byte:
                return(byte.Parse(input, CultureInfo.InvariantCulture));

            case (int)TypeCode.Char:
                return(char.Parse(input));

            case (int)TypeCode.DateTime:
                return(DateTime.Parse(input, CultureInfo.InvariantCulture));

            case (int)TypeCode.Decimal:
                return(decimal.Parse(input, CultureInfo.InvariantCulture));

            case (int)TypeCode.Double:
                return(double.Parse(input, CultureInfo.InvariantCulture));

            case (int)TypeCode.Int16:
                return(short.Parse(input, CultureInfo.InvariantCulture));

            case (int)TypeCode.Int32:
                return(int.Parse(input, CultureInfo.InvariantCulture));

            case (int)TypeCode.Int64:
                return(long.Parse(input, CultureInfo.InvariantCulture));

            case (int)TypeCode.SByte:
                return(sbyte.Parse(input, CultureInfo.InvariantCulture));

            case (int)TypeCode.Single:
                return(float.Parse(input, CultureInfo.InvariantCulture));

            case (int)TypeCode.UInt16:
                return(ushort.Parse(input, CultureInfo.InvariantCulture));

            case (int)TypeCode.UInt32:
                return(uint.Parse(input, CultureInfo.InvariantCulture));

            case (int)TypeCode.UInt64:
                return(ulong.Parse(input, CultureInfo.InvariantCulture));

            case (int)TypeCodeEx.ByteArray:
                return(Conversions.ToBytes(input));

            case (int)TypeCodeEx.DateTimeOffset:
                return(DateTimeOffset.Parse(input, CultureInfo.InvariantCulture));

            case (int)TypeCodeEx.Guid:
                return(Guid.Parse(input));

            case (int)TypeCodeEx.TimeSpan:
                return(TimeSpan.Parse(input, CultureInfo.InvariantCulture));

            default:
                throw new NotSupportedException();
            }
        }
Пример #2
0
        public virtual string ConvertToString(object input, out string typeName)
        {
            if (input == null || Convert.IsDBNull(input))
            {
                typeName = null;
                return(null);
            }

            var type = input.GetType();

            if (type.IsEnum)
            {
                typeName = type.AssemblyQualifiedName;
                return(Conversions.EnumToUInt64(input).ToString(CultureInfo.InvariantCulture));
            }

            var tc = Type.GetTypeCode(type);

            switch (tc)
            {
            case TypeCode.Empty:
                typeName = null;
                return(null);

            case TypeCode.String:
                typeName = null;
                return((string)input);

            case TypeCode.Object:
                if (type == typeof(byte[]))
                {
                    typeName = ((int)TypeCodeEx.ByteArray).ToString(CultureInfo.InvariantCulture);
                    return(Conversions.ToHexa((byte[])input));
                }

                if (type == typeof(Guid))
                {
                    typeName = ((int)TypeCodeEx.Guid).ToString(CultureInfo.InvariantCulture);
                    return(((Guid)input).ToString("N"));
                }

                if (type == typeof(TimeSpan))
                {
                    typeName = ((int)TypeCodeEx.TimeSpan).ToString(CultureInfo.InvariantCulture);
                    return(((TimeSpan)input).ToString());
                }

                if (type == typeof(DateTimeOffset))
                {
                    typeName = ((int)TypeCodeEx.DateTimeOffset).ToString(CultureInfo.InvariantCulture);
                    return(((DateTimeOffset)input).ToString(CultureInfo.InvariantCulture));
                }

                // hardcode some others?

                typeName = type.AssemblyQualifiedName;
                return(Conversions.ChangeType <string>(input, null, CultureInfo.InvariantCulture));

            default:
                typeName = ((int)tc).ToString(CultureInfo.InvariantCulture);
                return(Conversions.ChangeType <string>(input, null, CultureInfo.InvariantCulture));
            }
        }