Exemplo n.º 1
0
        /// <summary>
        /// 添加列表到流中
        /// </summary>
        private static bool AddListToStream(Type clazz, IList value, AdvancedObjectMarshalAsAttribute attr, BinaryWriter bw)
        {
            Type element = TypeUtils.GetArrayElement(clazz);

            if (element != null)
            {
                if (value == null)
                {
                    if (attr == null || attr.ArrayDoubleLength)
                    {
                        bw.Write(AdvancedObjectFormatter.As(BitConverter.GetBytes((short)-1), attr));
                    }
                    else
                    {
                        bw.Write((sbyte)-1);
                    }
                }
                else
                {
                    int size = value.Count;
                    if (attr != null && attr.SizeConst > 0)
                    {
                        size = attr.SizeConst;
                    }
                    else
                    if (attr == null || attr.ArrayDoubleLength)
                    {
                        bw.Write(AdvancedObjectFormatter.As(BitConverter.GetBytes((short)size), attr));
                    }
                    else
                    {
                        bw.Write((sbyte)size);
                    }
                    if (TypeUtils.IsBuffer(clazz))
                    {
                        bw.Write((byte[])value);
                    }
                    else
                    {
                        bool basic = ValueTypeUtils.IsBasicType(element);
                        for (int i = 0; i < size; i++)
                        {
                            object graph = value[i];
                            if (basic)
                            {
                                AdvancedObjectFormatter.AddValueToStream(element, graph, attr, bw);
                            }
                            else
                            {
                                AdvancedObjectFormatter.AddObjectToStream(graph, element, bw, attr);
                            }
                        }
                    }
                }
                return(true);
            }
            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 添加值到流中
        /// </summary>
        private static bool AddValueToStream(Type clazz, object value, AdvancedObjectMarshalAsAttribute attr, BinaryWriter bw)
        {
            int size = ValueTypeUtils.SizeBy(clazz);

            if (size > 0)
            {
                if (ValueTypeUtils.IsFloatType(clazz))
                {
                    bw.Write(AdvancedObjectFormatter.As(ValueTypeUtils.BinaryBy(clazz, Convert.ToDouble(value)), attr));
                }
                else
                {
                    if (ValueTypeUtils.IsDateTime(clazz))
                    {
                        value = Convert.ToDateTime(value).Ticks;
                    }
                    if (ValueTypeUtils.IsULong(clazz))
                    {
                        value = unchecked ((long)(ulong)value);
                    }
                    if (ValueTypeUtils.IsIPAddress(clazz))
                    {
                        value = BitConverter.ToInt32(((IPAddress)value).GetAddressBytes(), 0);
                    }
                    bw.Write(AdvancedObjectFormatter.As(ValueTypeUtils.BinaryBy(clazz, Convert.ToInt64(value), size), attr));
                }
                return(true);
            }
            else
            {
                if (AdvancedObjectFormatter.AddStringToStream(clazz, value as string, attr, bw))
                {
                    return(true);
                }
                if (AdvancedObjectFormatter.AddListToStream(clazz, value as IList, attr, bw))
                {
                    return(true);
                }
                if (AdvancedObjectFormatter.AddObjectToStream(value, clazz, bw, attr))
                {
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 3
0
 /// <summary>
 /// 序列化对象到二进制流
 /// </summary>
 /// <param name="graph">序列化的对象</param>
 /// <param name="s">序列化对象存储的流</param>
 public static void Serialize(object graph, Stream s)
 {
     if (graph == null)
     {
         throw new ArgumentNullException("graph");
     }
     if (s == null)
     {
         throw new ArgumentNullException("s");
     }
     if (!s.CanWrite)
     {
         throw new ArgumentException("s");
     }
     using (BinaryWriter bw = new BinaryWriter(s))
     {
         Type clazz = graph.GetType();
         AdvancedObjectFormatter.AddObjectToStream(graph, clazz, bw, null);
     }
 }