Beispiel #1
0
        /// <summary>
        /// Serialize the object to the stream.
        /// </summary>
        /// <param name="obj">The object to be Serialize</param>
        /// <param name="min">The min must be -1.</param>
        /// <param name="max">The max must be -1.</param>
        /// <param name="fillValue">This parameter must be null.</param>
        /// <param name="stream">The stream to serialize.</param>
        /// <returns>The size have been Serialize.</returns>
        private static int Serialize(object obj, int min, int max, byte[] fillValue, Stream stream)
        {
            int size = 0;
            int i;

            if (obj == null)
            {
                size += FillStream(null, min, max, fillValue, stream);
            }
            else if (SerializableObjectAttribute.IsSerializableObject(obj))
            {
                SerializableObjectAttribute att = SerializableObjectAttribute.GetSerializableObject(obj);
                if (att.UseSelfSerialize)
                {
                    IStructSerializable serial = obj as IStructSerializable;
                    size += serial.Serialize(stream);
                }
                else
                {
                    List <FieldInfo> fields = FieldHelper.FilterFields(obj.GetType());
                    for (i = 0; i < fields.Count; i++)
                    {
                        SerializableFieldAttribute sf = FieldHelper.GetSerializableField(fields[i]);
                        size += Serialize(
                            fields[i].GetValue(obj),
                            sf.MinAllocSize,
                            sf.MaxAllocSize,
                            null,
                            stream);
                    }
                }
            }
            else if (obj is IList)
            {
                IList lst = obj as IList;
                for (i = 0; i < lst.Count; i++)
                {
                    size += Serialize(lst[i], min, max, fillValue, stream);
                }
            }
            else if (obj.GetType().IsValueType)
            {
                int    bufferSize = Marshal.SizeOf(obj);
                byte[] buffer     = new byte[bufferSize];
                IntPtr p          = Marshal.AllocHGlobal(bufferSize);
                Marshal.StructureToPtr(obj, p, false);
                Marshal.Copy(p, buffer, 0, bufferSize);
                size += FillStream(buffer, min, max, fillValue, stream);
                Marshal.FreeHGlobal(p);
            }
            else if (obj is string)
            {
                AdapterHelper.Site.Assert.Fail("Method is not implemented.");
            }
            else
            {
                AdapterHelper.Site.Assert.Fail("This type of 'obj' is not supported, its type is {0}.", obj.GetType().Name);
            }

            return(size);
        }
 /// <summary>
 /// Get StringEncoding attribute from an object.
 /// </summary>
 /// <param name="obj">The object.</param>
 /// <returns>A StringEncoding instance have been specified to the object.</returns>
 public static StringEncodingAttribute GetStringEncoding(object obj)
 {
     return(FieldHelper.GetFirstCustomAttribute <StringEncodingAttribute>(obj, false));
 }