Пример #1
0
 /// <summary>
 /// 获取引用对象的类型
 /// </summary>
 /// <param name="singleType"></param>
 /// <param name="valueType"></param>
 /// <returns></returns>
 protected Type DeserializeNullAssemblyType(Type singleType, string valueType)
 {
     try
     {
         singleType = ByteConverterFactory.GetType(valueType);
     }
     catch
     {
     }
     return(singleType);
 }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="singleType"></param>
        protected void SerializeNullAssemblyType(MemoryStream stream, Type singleType)
        {
            string AssemblyFullName = singleType.Assembly.FullName;

            AssemblyFullName = AssemblyFullName.Substring(0, AssemblyFullName.IndexOf(","));
            if (!AssemblyFullName.StartsWith("mscorlib"))
            {
                byte[] bytes   = Encoding.UTF8.GetBytes("");
                byte[] buffer3 = ByteConverter.ToBytes <int>(bytes.Length);
                stream.Write(buffer3, 0, buffer3.Length);
                stream.Write(bytes, 0, bytes.Length);
            }
            else
            {
                byte[] bytes   = Encoding.UTF8.GetBytes(ByteConverterFactory.GetString(singleType));
                byte[] buffer3 = ByteConverter.ToBytes <int>(bytes.Length);
                stream.Write(buffer3, 0, buffer3.Length);
                stream.Write(bytes, 0, bytes.Length);
            }
        }
Пример #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="type"></param>
        /// <param name="obj"></param>
        protected void DoSerialize(MemoryStream stream, Type type, object obj)
        {
            Type valueType = null;

            if (obj != null && !type.IsNullableType())
            {
                valueType = obj.GetType();
            }

            Type singleType = (valueType == null ? type : valueType);

            if (DepthType == EnumDepthType.Depth)
            {
                if (obj != null)
                {
                    singleType = singleType.GetUnderlyingType();
                }
                SerializeAssemblyType(stream, singleType);
            }
            else if (DepthType == EnumDepthType.Normal)
            {
                if (obj != null)
                {
                    singleType = singleType.GetUnderlyingType();
                }
                SerializeNullAssemblyType(stream, singleType);
            }

            byte[] buffer;
            if (ByteConverter.SupportType(singleType))
            {
                buffer = ByteConverterFactory.ToBytes(singleType, obj);
                stream.Write(buffer, 0, buffer.Length);
            }
            else if (ByteNullableConverter.SupportType(singleType))
            {
                buffer = ByteConverterFactory.ToBytes(singleType, obj);
                stream.Write(buffer, 0, buffer.Length);
            }
            else if (singleType == typeof(DataTable))
            {
                string table = DataTableSerializer.SerializeDataTableXml((DataTable)obj, "SerializeDataTable");
                if (obj == null)
                {
                    buffer = ByteConverter.ToBytes <int>(-1);
                    stream.Write(buffer, 0, buffer.Length);
                }
                else
                {
                    SerializeString(stream, table);
                }
            }
            else if (singleType == typeof(string))
            {
                if (obj == null)
                {
                    buffer = ByteConverter.ToBytes <int>(-1);
                    stream.Write(buffer, 0, buffer.Length);
                }
                else
                {
                    SerializeString(stream, obj);
                }
            }

            else if (obj is _Type)
            {
                if (obj == null)
                {
                    buffer = ByteConverter.ToBytes <int>(-1);
                    stream.Write(buffer, 0, buffer.Length);
                }
                else
                {
                    SerializeString(stream, ((_Type)obj).AssemblyQualifiedName);
                }
            }
            else if (singleType == typeof(byte[]))
            {
                if (obj == null)
                {
                    buffer = ByteConverter.ToBytes <int>(-1);
                    stream.Write(buffer, 0, buffer.Length);
                }
                else
                {
                    SerializeBytes(stream, obj);
                }
            }
            else
            {//
                if (IsGenericTypeList(singleType))
                {
                    if (obj == null)
                    {
                        buffer = ByteConverter.ToBytes <int>(-1);
                        stream.Write(buffer, 0, 4);
                    }
                    else
                    {
                        SerializeList(stream, obj, singleType);
                    }
                }
                else if (IsGenericTypeDictionary(singleType))
                {
                    if (obj == null)
                    {
                        buffer = ByteConverter.ToBytes <int>(-1);
                        stream.Write(buffer, 0, 4);
                    }
                    else
                    {
                        SerializeDictionary(stream, obj, singleType);
                    }
                }
                else if (singleType.IsArray)
                {
                    if (obj == null)
                    {
                        buffer = ByteConverter.ToBytes <int>(-1);
                        stream.Write(buffer, 0, 4);
                    }
                    else
                    {
                        SerializeArray(stream, obj, singleType);
                    }
                }
                else
                {
                    if (singleType.IsGenericType)
                    {
                        throw new DMSFrameException(string.Format("{0}", singleType));
                    }
                    this.DoSerializeComplicatedType(singleType, obj, stream);
                }
            }
        }
Пример #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="baseType"></param>
        /// <param name="type"></param>
        /// <param name="valueType"></param>
        /// <param name="buff"></param>
        /// <param name="offset"></param>
        /// <returns></returns>
        protected object DoDeserialize(Type baseType, Type type, string valueType, byte[] buff, ref int offset)
        {
            if (DepthType == EnumDepthType.Depth && string.IsNullOrEmpty(valueType))
            {
                int count = ByteConverter.Parse <int>(buff, ref offset);
                if (count > -1)
                {
                    valueType = Encoding.UTF8.GetString(buff, offset, count);
                    offset   += count;
                }
                if (string.IsNullOrEmpty(valueType))
                {
                    throw new DMSFrameException("深度检查Type失败!");
                }
                return(DoDeserialize(baseType, type, valueType, buff, ref offset));
            }
            else if (DepthType == EnumDepthType.Normal && string.IsNullOrEmpty(valueType))
            {
                int count = ByteConverter.Parse <int>(buff, ref offset);
                if (count > -1)
                {
                    if (count == 0)
                    {
                        valueType = type.FullName.ToString();
                    }
                    else
                    {
                        valueType = Encoding.UTF8.GetString(buff, offset, count);
                        offset   += count;
                    }
                }
                if (string.IsNullOrEmpty(valueType))
                {
                    throw new DMSFrameException("深度检查Type失败!");
                }
                return(DoDeserialize(baseType, type, valueType, buff, ref offset));
            }
            object obj5;
            Type   singleType = type;

            if (!string.IsNullOrEmpty(valueType))
            {
                if (DepthType == EnumDepthType.Normal)
                {
                    singleType = DeserializeNullAssemblyType(singleType, valueType);
                    if (singleType == null)
                    {
                        singleType = type;
                    }
                }
                else
                {
                    singleType = DeserializeAssemblyType(singleType, valueType);
                    if (singleType == null)
                    {
                        singleType = type;
                    }
                }
            }


            if (ByteConverter.SupportType(singleType))
            {
                return(ByteConverterFactory.Parse(singleType, buff, ref offset));
            }
            if (ByteNullableConverter.SupportType(singleType))
            {
                return(ByteConverterFactory.Parse(singleType, buff, ref offset));
            }
            if (singleType == typeof(string))
            {
                return(DeserializeString(buff, ref offset));
            }
            if (singleType.FullName == "System.Type" || singleType.FullName == "System.RuntimeType")
            {
                string value = DeserializeString(buff, ref offset);
                return(Type.GetType(value));
            }
            if (singleType == typeof(byte[]))
            {
                return(DeserializeBytes(buff, ref offset));
            }

            if (IsGenericTypeList(singleType))
            {
                return(DeserializeList(baseType, buff, ref offset, singleType));
            }
            if (IsGenericTypeDictionary(singleType))
            {
                return(DeserializeDictionary(baseType, buff, ref offset, singleType));
            }
            if (singleType == typeof(DataTable))
            {
                string table = DeserializeString(buff, ref offset);
                return(DataTableSerializer.DeserializeDataTable(table));
            }
            if (singleType.IsArray)
            {
                return(DeserializeArray(baseType, buff, ref offset, singleType));
            }

            if (singleType.IsGenericType)
            {
                throw new DMSFrameException(string.Format("{0}", singleType));
            }
            try
            {
                obj5 = this.DoDeserializeComplicatedType(baseType, singleType, buff, ref offset);
            }
            catch (Exception exception)
            {
                throw new DMSFrameException(string.Format("{0}-{1}", exception.Message, singleType), exception);
            }
            return(obj5);
        }
Пример #5
0
 /// <summary>
 /// 以结构对象的形式返回指定的字节数组
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="t"></param>
 /// <returns></returns>
 public static byte[] ToBytes <T>(T t)
 {
     return(ByteConverterFactory.ToBytes(typeof(T), t));
 }
Пример #6
0
 /// <summary>
 /// 读取buffer的字节返回相应的对象
 /// </summary>
 /// <typeparam name="T">类型</typeparam>
 /// <param name="buff">字节数组</param>
 /// <param name="offset">开始位置</param>
 /// <returns></returns>
 public static T Parse <T>(byte[] buff, ref int offset) where T : struct
 {
     return((T)ByteConverterFactory.Parse(typeof(T), buff, ref offset));
 }