Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public object ReadEventObj(Type type)
        {
            var constructor = type.GetConstructor(Type.EmptyTypes);
            var obj         = constructor.Invoke(null) as IYiDianSeralize;
            var read        = new ReadStream(orginal.Slice(Offset));

            obj.BytesTo(read);
            Offset += read.DataSize;
            return(obj);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="data">字节数组</param>
        /// <param name="type">消息类型</param>
        /// <returns>消息</returns>
        public object DeserializeObject(ReadOnlyMemory <byte> data, Type type)
        {
            bool isArray = false;
            var  ilist   = type.GetInterface(typeof(IList <>).FullName);

            if (ilist != null)
            {
                isArray = true;
                type    = ilist.GetGenericArguments()[0];
            }
            if (data.Length == 0)
            {
                if (!isArray && type.IsValueType)
                {
                    return(0);
                }
                return(null);
            }
            var stream = new ReadStream(data)
            {
                Encoding = encoding
            };

            if (!isArray)
            {
                if (type.IsValueType)
                {
                    if (type == typeof(byte))
                    {
                        return(stream.ReadByte());
                    }
                    else if (type == typeof(short))
                    {
                        return(stream.ReadInt16());
                    }
                    else if (type == typeof(ushort))
                    {
                        return(stream.ReadUInt16());
                    }
                    else if (type == typeof(int))
                    {
                        return(stream.ReadInt32());
                    }
                    else if (type == typeof(uint))
                    {
                        return(stream.ReadUInt32());
                    }
                    else if (type == typeof(long))
                    {
                        return(stream.ReadInt64());
                    }
                    else if (type == typeof(ulong))
                    {
                        return(stream.ReadUInt64());
                    }
                    else if (type == typeof(double))
                    {
                        return(stream.ReadDouble());
                    }
                    else if (type == typeof(DateTime))
                    {
                        return(stream.ReadDate());
                    }
                    else if (type == typeof(bool))
                    {
                        return(stream.ReadByte() == 1);
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }
                }
                else if (type == typeof(string))
                {
                    return(stream.ReadString());
                }
                else
                {
                    if (type.GetInterface(typeof(IYiDianSeralize).FullName) == null)
                    {
                        throw new ArgumentException(nameof(type), "event type must instance of IYiDianSeralize");
                    }
                    return(stream.ReadEventObj(type));
                }
            }
            if (type.IsValueType)
            {
                if (type == typeof(byte))
                {
                    return(stream.ReadArrayByte().ToArray());
                }
                else if (type == typeof(short))
                {
                    return(stream.ReadArrayInt16());
                }
                else if (type == typeof(ushort))
                {
                    return(stream.ReadArrayUInt16());
                }
                else if (type == typeof(int))
                {
                    return(stream.ReadArrayInt32());
                }
                else if (type == typeof(uint))
                {
                    return(stream.ReadArrayUInt32());
                }
                else if (type == typeof(long))
                {
                    return(stream.ReadArrayInt64());
                }
                else if (type == typeof(ulong))
                {
                    return(stream.ReadArrayUInt64());
                }
                else if (type == typeof(double))
                {
                    return(stream.ReadArrayDouble());
                }
                else if (type == typeof(DateTime))
                {
                    return(stream.ReadArrayDate());
                }
                else if (type == typeof(bool))
                {
                    return(stream.ReadArrayBool());
                }
                else
                {
                    throw new NotImplementedException();
                }
            }
            else if (type == typeof(string))
            {
                return(stream.ReadArrayString());
            }
            else
            {
                return(stream.ReadArray(type));
            }
        }