Example #1
0
        /// <summary>
        /// Deserializes the given object.
        /// </summary>
        /// <param name="stream">Stream containing data.</param>
        /// <param name="type">The type of the object to create.</param>
        /// <returns>The deserialized object.</returns>
        public object Deserialize(Type type, SerializeStream stream)
        {
            FieldInfo[] infos = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            object      obj   = Activator.CreateInstance(type, true);

            for (int i = 0; i < infos.Length; i++)
            {
                FieldInfo info = infos[i];
                if (info.IsDefined(typeof(SerializeAttribute), false))
                {
                    SerializeAttribute attribute = (SerializeAttribute)
                                                   info.GetCustomAttributes(typeof(SerializeAttribute), false)[0];

                    if (attribute.Primitive)
                    {
                        info.SetValue(obj, stream.Read(info.Name, info.FieldType));
                    }
                    else
                    {
                        info.SetValue(obj, stream.ReadObject(info.Name));
                    }
                }
            }

            PropertyInfo[] pInfos = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            for (int i = 0; i < pInfos.Length; i++)
            {
                PropertyInfo pInfo = pInfos[i];
                if (pInfo.IsDefined(typeof(SerializeAttribute), false))
                {
                    SerializeAttribute attribute = (SerializeAttribute)
                                                   pInfo.GetCustomAttributes(typeof(SerializeAttribute), false)[0];

                    if (pInfo.CanWrite)
                    {
                        if (attribute.Primitive)
                        {
                            pInfo.SetValue(obj, stream.Read(pInfo.Name, pInfo.PropertyType), null);
                        }
                        else
                        {
                            pInfo.SetValue(obj, stream.ReadObject(pInfo.Name), null);
                        }
                    }
                }
            }
            return(obj);
        }
 /// <summary>
 /// Reads the four (4) header bytes from the input stream.
 /// </summary>
 /// <param name="recordLength">The length of the record as a 16 bit unsigned integer.</param>
 /// <param name="recordTypeCode">The Type and Subtype codes for the current record.</param>
 protected virtual void ReadHeader(out ushort recordLength, out ushort recordTypeCode)
 {
     byte[] buffer = new byte[2];
     // Read record length bytes into buffer
     SerializeStream.Read(buffer, 0, 2);
     recordLength = Converter.ToUInt16(buffer);
     // Read record type bytes into buffer
     SerializeStream.Read(buffer, 0, 2);
     recordTypeCode = (Converter.ToUInt16(buffer));
 }
Example #3
0
 protected byte[] ReadBytes(int count)
 {
     byte[] buffer = new byte[count];
     SerializeStream.Read(buffer, 0, count);
     return(buffer);
 }