Ejemplo n.º 1
0
        public virtual object ToRemoteValue(AbapDataType remoteType, object value)
        {
            if (value == null)
            {
                return(null);
            }

            Type valueType = value.GetType();

            if (valueType == typeof(Boolean))
            {
                return(AbapBool.ToString((Boolean)value));
            }

            if (valueType.Equals(typeof(double)))
            {
                return(((double)value).ToString(this.GetNumberFormat()));
            }

            if (valueType.Equals(typeof(float)))
            {
                return(((float)value).ToString(this.GetNumberFormat()));
            }

            if (valueType.Equals(typeof(Decimal)))
            {
                return(((Decimal)value).ToString(this.GetNumberFormat()));
            }

            if (valueType.Equals(typeof(Double)))
            {
                return(((Double)value).ToString(this.GetNumberFormat()));
            }

            if (valueType.IsEnum)
            {
                return(ConvertFromEnum(remoteType, value));
            }

            if (remoteType == AbapDataType.DATE)
            {
                return(AbapDateTime.ToDateString((DateTime)value));
            }

            if (remoteType == AbapDataType.TIME)
            {
                return(AbapDateTime.ToTimeString((DateTime)value));
            }

            return(value);
        }
Ejemplo n.º 2
0
        public virtual object FromRemoteValue(Type type, object value)
        {
            if (type.IsEnum)
            {
                return(this.ConvertToEnum(type, value));
            }

            if (type.Equals(typeof(string)))
            {
                return(value == null ? "" : value.ToString());
            }

            if (value == null || value.ToString().Equals(""))
            {
                if (type.IsNullable())
                {
                    return(null);
                }

                return(Activator.CreateInstance(type));
            }

            if (type.Equals(typeof(Boolean)))
            {
                return(AbapBool.FromString(value.ToString()));
            }

            if (type.Equals(typeof(DateTime)))
            {
                return(AbapDateTime.FromString(value.ToString()));
            }

            if (type.Equals(typeof(DateTime?)))
            {
                return(AbapDateTime.FromString(value.ToString(), true));
            }

            if (type.Equals(typeof(Decimal)))
            {
                if (value.ToString().StartsWith("*"))
                {
                    throw new SharpRfcException(string.Format("SAP truncated value {0}. Operation aborted", value));
                }

                return(Convert.ToDecimal(value, value.ToString().Contains(",") ? this.CommaDecimalNumberFormat : this.PeriodDecimalNumberFormat));
            }

            if (type.Equals(typeof(Double)))
            {
                if (value.ToString().StartsWith("*"))
                {
                    throw new SharpRfcException(string.Format("SAP truncated value {0}. Operation aborted", value));
                }

                return(Convert.ToDouble(value, value.ToString().Contains(",") ? this.CommaDecimalNumberFormat : this.PeriodDecimalNumberFormat));
            }

            if (type.Equals(typeof(byte[])))
            {
                return(this.ToBytes(value));
            }

            if (type.Equals(typeof(Stream)))
            {
                return(new MemoryStream(this.ToBytes(value)));
            }

            return(Convert.ChangeType(value, type));
        }