Ejemplo n.º 1
0
        public object TryConvertValue(object value)
        {
            if (value is string)
            {
                var s = (string)value;
                if (this.TypeHelper.Type == typeof(Guid))
                {
                    Guid g;
                    if (Guid.TryParse(s, out g))
                    {
                        return(g);
                    }
                }
                else if (this.TypeHelper.Type == typeof(bool))
                {
                    var val = s.ToLower();
                    if (val == "true" || val == "on" || val == "1" || val == "yes")
                    {
                        return(true);
                    }
                    else if (val == "false" || val == "off" || val == "0" || val == "no")
                    {
                        return(false);
                    }
                }
                else if (string.IsNullOrWhiteSpace((string)value))
                {
                    if (nullable)
                    {
                        return(null);
                    }
                    else if (new DataType[] { DataType.Decimal, DataType.Int32, DataType.Int64, DataType.Float64 }.Contains(schema.DataType))
                    {
                        value = 0;
                    }
                }
                else if (this.TypeHelper.Type == typeof(ItemId))
                {
                    ItemId itemPath;
                    if (ItemIdExtensions.TryParse((string)value, out itemPath))
                    {
                        return(itemPath);
                    }

                    try
                    {
                        return(ItemIdExtensions.FromBase64Url(s));
                    }
                    catch (FormatException)
                    {
                    }
                    catch (ItemIdException)
                    {
                    }
                }
                else if (this.TypeHelper.Type == typeof(byte[]))
                {
                    try
                    {
                        return(Convert.FromBase64String(s));
                    }
                    catch (FormatException)
                    {
                    }
                }

                if (nullable && s.Equals("null", StringComparison.OrdinalIgnoreCase))
                {
                    return(null);
                }
            }

            try
            {
                return(Convert.ChangeType(value, TypeHelper.Type));
            }
            catch (Exception e)
            {
                throw new ArgumentException("Field type is not compatible", "value", e);
            }
        }
Ejemplo n.º 2
0
        public object Get()
        {
            if (buffer == null)
            {
                return(null);
            }

            var dataType = this.schema.DataType;

            switch (dataType)
            {
            case DataType.Boolean:
                return(BitConverter.ToBoolean(buffer, offset));

            case DataType.Int32:
            case DataType.Choice:
                return(BitConverter.ToInt32(buffer, offset));

            case DataType.Int64:
                return(BitConverter.ToInt64(buffer, offset));

            case DataType.Float64:
                return(BitConverter.ToDouble(buffer, offset));

            case DataType.Decimal:
                return(new Decimal(new int[] {
                    BitConverter.ToInt32(buffer, offset),
                    BitConverter.ToInt32(buffer, offset + 4),
                    BitConverter.ToInt32(buffer, offset + 8),
                    BitConverter.ToInt32(buffer, offset + 12)
                }));

            case DataType.DateTime:
                return(DateTime.FromBinary(BitConverter.ToInt64(buffer, offset)));

            case DataType.TimeSpan:
                return(new TimeSpan(BitConverter.ToInt64(buffer, offset)));

            case DataType.Guid:
            {
                var b = new byte[16];
                Array.Copy(buffer, offset, b, 0, 16);
                return(new Guid(b));
            }

            case DataType.String:
            case DataType.Binary:
            case DataType.ItemPath:
            {
                int sizeWidth;
                int l = DecodeLength(offset, out sizeWidth);
                if (dataType == DataType.String)
                {
                    return(Encoding.UTF8.GetString(buffer, offset + sizeWidth, l - sizeWidth));
                }
                else if (dataType == DataType.ItemPath)
                {
                    return(ItemIdExtensions.Read(buffer, offset + sizeWidth, l - sizeWidth));
                }
                else
                {
                    var b = new byte[l - sizeWidth];
                    Buffer.BlockCopy(buffer, offset + sizeWidth, b, 0, b.Length);
                    return(b);
                }
            }

            case DataType.Class:
                switch (schema.Id)
                {
                case (int)BuiltInSchema.Variable:
                    return(AccessVariable());

                case (int)BuiltInSchema.Money:
                    return(new Money(this.GoTo(1 /*"Currency"*/).Get <string>(), this.GoTo(0 /*"Amount"*/).Get <decimal>()));

                case (int)BuiltInSchema.GeoPosition:
                    return(new GeoPosition(this.GoTo(0 /*"Longitude"*/).Get <double>(), this.GoTo(1 /*"Latitude"*/).Get <double>()));

                case (int)BuiltInSchema.Measurement:
                    return(new Measurement(this.GoTo(1 /*Unit*/).Get <string>(), this.GoTo(0 /*Value*/).Get <double>()));

                case (int)BuiltInSchema.ListOfDateTimeOffset:
                    return(new DateTimeOffset(this.GoTo(0 /*Time*/).Get <DateTime>(), this.GoTo(1 /*Offset*/).Get <TimeSpan>()));
                }
                break;
            }

            return(this);
        }