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);
        }
Example #2
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)
        {
            Type elementType = stream.ReadType("elementType");
            int  length      = stream.ReadInt("length");

            System.Array array = System.Array.CreateInstance(elementType, length);
            for (int i = 0; i < array.Length; i++)
            {
                array.SetValue(stream.ReadObject(null), i);
            }
            return(array);
        }