Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="obj"></param>
        private object DecodeSingleValue(JObject obj)
        {
            object re = null;

            var pps  = obj.Properties().ToList();
            int bval = 0;

            if (pps.Count > 0)
            {
                bval = int.Parse(pps[0].Name);
                var val = pps[0].Value;

                switch ((TagType)bval)
                {
                case TagType.Bool:
                    re = val.ToObject <bool>();
                    break;

                case TagType.Byte:
                    re = val.ToObject <byte>();
                    break;

                case TagType.DateTime:
                    re = val.ToObject <DateTime>();
                    break;

                case TagType.Double:
                    re = val.ToObject <double>();
                    break;

                case TagType.Float:
                    re = val.ToObject <float>();
                    break;

                case TagType.Int:
                    re = val.ToObject <int>();
                    break;

                case TagType.Long:
                    re = val.ToObject <long>();
                    break;

                case TagType.Short:
                    re = val.ToObject <short>();
                    break;

                case TagType.String:
                    re = val.ToObject <string>();
                    break;

                case TagType.UInt:
                    re = val.ToObject <uint>();
                    break;

                case TagType.ULong:
                    re = val.ToObject <ulong>();
                    break;

                case TagType.UShort:
                    re = val.ToObject <ushort>();
                    break;

                case TagType.IntPoint:
                    var vvs = val.ToObject <int[]>();
                    re = new IntPoint()
                    {
                        X = vvs[0], Y = vvs[1]
                    };
                    break;

                case TagType.IntPoint3:
                    var vvs1 = val.ToObject <int[]>();
                    re = new IntPoint3()
                    {
                        X = vvs1[0], Y = vvs1[1], Z = vvs1[2]
                    };
                    break;

                case TagType.UIntPoint:
                    var vvs2 = val.ToObject <uint[]>();
                    re = new UIntPoint()
                    {
                        X = vvs2[0], Y = vvs2[1]
                    };
                    break;

                case TagType.UIntPoint3:
                    var vvs3 = val.ToObject <uint[]>();
                    re = new UIntPoint3()
                    {
                        X = vvs3[0], Y = vvs3[1], Z = vvs3[2]
                    };
                    break;

                case TagType.LongPoint:
                    var vvs4 = val.ToObject <long[]>();
                    re = new LongPoint()
                    {
                        X = vvs4[0], Y = vvs4[1]
                    };
                    break;

                case TagType.LongPoint3:
                    var vvs5 = val.ToObject <long[]>();
                    re = new LongPoint3()
                    {
                        X = vvs5[0], Y = vvs5[1], Z = vvs5[2]
                    };
                    break;

                case TagType.ULongPoint:
                    var vvs6 = val.ToObject <ulong[]>();
                    re = new ULongPoint()
                    {
                        X = vvs6[0], Y = vvs6[1]
                    };
                    break;

                case TagType.ULongPoint3:
                    var vvs7 = val.ToObject <ulong[]>();
                    re = new ULongPoint3()
                    {
                        X = vvs7[0], Y = vvs7[1], Z = vvs7[2]
                    };
                    break;
                }
            }
            return(re);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="tag"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        private byte[] ConvertToBytes(Tagbae tag, object value)
        {
            switch (tag.Type)
            {
            case TagType.Bool:
                return(BitConverter.GetBytes((bool)value));

            case TagType.Byte:
                return(BitConverter.GetBytes(Convert.ToByte(value)));

            case TagType.DateTime:
                return(BitConverter.GetBytes(Convert.ToInt64(value)));

            case TagType.Double:
                return(BitConverter.GetBytes(Convert.ToDouble(value)));

            case TagType.Float:
                return(BitConverter.GetBytes(Convert.ToSingle(value)));

            case TagType.Int:
                return(BitConverter.GetBytes(Convert.ToInt32(value)));

            case TagType.Long:
                return(BitConverter.GetBytes(Convert.ToInt64(value)));

            case TagType.Short:
                return(BitConverter.GetBytes(Convert.ToInt16(value)));

            case TagType.String:
                return(Encoding.UTF8.GetBytes(value.ToString()));

            case TagType.UInt:
                return(BitConverter.GetBytes(Convert.ToUInt32(value)));

            case TagType.ULong:
                return(BitConverter.GetBytes(Convert.ToUInt64(value)));

            case TagType.UShort:
                return(BitConverter.GetBytes(Convert.ToUInt16(value)));

            case TagType.IntPoint:
                IntPoint ival = (IntPoint)value;
                byte[]   val  = new byte[8];
                BitConverter.GetBytes(ival.X).CopyTo(val, 0);
                BitConverter.GetBytes(ival.Y).CopyTo(val, 4);
                return(val);

            case TagType.IntPoint3:
                IntPoint3 ival3 = (IntPoint3)value;
                val = new byte[12];
                BitConverter.GetBytes(ival3.X).CopyTo(val, 0);
                BitConverter.GetBytes(ival3.Y).CopyTo(val, 4);
                BitConverter.GetBytes(ival3.Z).CopyTo(val, 8);
                return(val);

            case TagType.UIntPoint3:
                UIntPoint3 uival3 = (UIntPoint3)value;
                val = new byte[12];
                BitConverter.GetBytes(uival3.X).CopyTo(val, 0);
                BitConverter.GetBytes(uival3.Y).CopyTo(val, 4);
                BitConverter.GetBytes(uival3.Z).CopyTo(val, 8);
                return(val);

            case TagType.UIntPoint:
                UIntPoint uival = (UIntPoint)value;
                val = new byte[8];
                BitConverter.GetBytes(uival.X).CopyTo(val, 0);
                BitConverter.GetBytes(uival.Y).CopyTo(val, 4);
                return(val);

            case TagType.LongPoint:
                LongPoint lval = (LongPoint)value;
                val = new byte[16];
                BitConverter.GetBytes(lval.X).CopyTo(val, 0);
                BitConverter.GetBytes(lval.Y).CopyTo(val, 8);
                return(val);

            case TagType.LongPoint3:
                LongPoint3 lval3 = (LongPoint3)value;
                val = new byte[24];
                BitConverter.GetBytes(lval3.X).CopyTo(val, 0);
                BitConverter.GetBytes(lval3.Y).CopyTo(val, 8);
                BitConverter.GetBytes(lval3.Z).CopyTo(val, 16);
                return(val);

            case TagType.ULongPoint:
                ULongPoint ulval = (ULongPoint)value;
                val = new byte[16];
                BitConverter.GetBytes(ulval.X).CopyTo(val, 0);
                BitConverter.GetBytes(ulval.Y).CopyTo(val, 8);
                return(val);

            case TagType.ULongPoint3:
                ULongPoint3 ulval3 = (ULongPoint3)value;
                val = new byte[24];
                BitConverter.GetBytes(ulval3.X).CopyTo(val, 0);
                BitConverter.GetBytes(ulval3.Y).CopyTo(val, 8);
                BitConverter.GetBytes(ulval3.Z).CopyTo(val, 16);
                return(val);
            }
            return(null);
        }