public static void Save(this IBinarySerializable @this, string filePath)
 {
     using (var writer = new EndianBinaryWriter(new MemoryStream(), Endianness.Little))
     {
         @this.Write(writer);
         using (var fileStream = File.Create(filePath))
         {
             writer.BaseStream.Position = 0;
             writer.BaseStream.CopyTo(fileStream);
         }
     }
 }
Esempio n. 2
0
        public static void Serialize(object source, BinaryWriter writer)
        {
            // Ensure the source supports the correct interface.

            IBinarySerializable serializable = source as IBinarySerializable;

            if (serializable == null)
            {
                return;
            }

            writer.Write(source.GetType().AssemblyQualifiedName);
            serializable.Write(writer);
        }
Esempio n. 3
0
        private EventParameter CloneParameter()
        {
            // Determine whether the value supports ICloneable.

            System.ICloneable cloneable = m_value as System.ICloneable;
            if (cloneable != null)
            {
                return(new EventParameter(m_name, cloneable.Clone()));
            }

            // Determine whether the value supports IBinarySerializable.

            IBinarySerializable serializable = m_value as IBinarySerializable;

            if (serializable != null)
            {
                MemoryStream stream = new MemoryStream();
                using (BinaryWriter writer = new BinaryWriter(stream))
                {
                    serializable.Write(writer);
                    return(new EventParameter(m_name, new ClassInfo(m_value.GetType()), stream.ToArray(), ValueFormat.LinkMeBinary));
                }
            }

            // Determine whether the value supports IXmlSerializable.

            IXmlSerializable xmlSerializable = m_value as IXmlSerializable;

            if (xmlSerializable != null)
            {
                return(new EventParameter(m_name, new ClassInfo(m_value.GetType()), XmlSerializer.Serialize(xmlSerializable), ValueFormat.LinkMeXml));
            }

            // Determine whether the value can be XmlSerialized.

            try
            {
                return(new EventParameter(m_name, new ClassInfo(m_value.GetType()), XmlSerializer.Serialize(m_value), ValueFormat.SystemXml));
            }
            catch (System.Exception)
            {
            }

            // Just return the string representation.

            return(new EventParameter(m_name, new ClassInfo(m_value.GetType()), m_value.ToString(), ValueFormat.String));
        }
Esempio n. 4
0
 public static string TrySerializeToString(IBinarySerializable obj)
 {
     try
     {
         using (MemoryStream memoryStream = new MemoryStream())
         {
             BinaryWriter writer = new BinaryWriter((Stream)memoryStream);
             obj.Write(writer);
             memoryStream.Position = 0L;
             return(CacheManager.AsciiToString(new BinaryReader((Stream)memoryStream).ReadBytes((int)memoryStream.Length)));
         }
     }
     catch (Exception ex)
     {
         Logger.Instance.Error("TrySerializeToString.TryDeserialize failed.", ex);
     }
     return("");
 }
Esempio n. 5
0
        /// <summary>
        /// Implements the ISerializable.GetObjectData() method for an object that implements IBinarySerializable.
        /// </summary>
        /// <param name="value">The object instance to be serialized.</param>
        /// <param name="info">The SerializationInfo object passed to the serialized object's
        /// ISerializable.GetObjectData() method.</param>
        public static void WriteObjectDataForBinarySerializable(IBinarySerializable value, SerializationInfo info)
        {
            const string method = "WriteObjectDataForBinarySerializable";

            if (value == null)
            {
                throw new Exceptions.NullParameterException(typeof(BinarySerializer), method, "value");
            }
            if (info == null)
            {
                throw new Exceptions.NullParameterException(typeof(BinarySerializer), method, "info");
            }

            MemoryStream stream = new MemoryStream();

            using (BinaryWriter writer = new BinaryWriter(stream))
            {
                value.Write(writer);
            }

            info.AddValue(Constants.Serialization.BinarySerializableStreamKey, stream.ToArray());
        }
Esempio n. 6
0
        private IBinarySerializable SerializeAndDeserializeObject(IBinarySerializable obj, Type objType)
        {
            dynamic buffer;

            using (var stream = new MemoryStream())
            {
                var binaryWriter = new BinaryWriter(stream);

                obj.Write(binaryWriter);

                buffer = stream.ToArray();
            }

            var returnObj = Activator.CreateInstance(objType) as IBinarySerializable;

            using (var readStream = new MemoryStream(buffer))
            {
                var binaryReader = new BinaryReader(readStream);
                returnObj.Read(binaryReader);
            }

            return(returnObj);
        }
Esempio n. 7
0
 /// <summary>
 /// Writes an item implementing the IBinarySerializable interface to a binary writer. Included for symmetry with standard Writer.Write(X) calls.
 /// </summary>
 /// <param name="Writer">Writer to serialize to</param>
 /// <param name="Item">The item to write</param>
 public static void Write(this BinaryWriter Writer, IBinarySerializable Item)
 {
     Item.Write(Writer);
 }
 public static void Save(this IBinarySerializable @this, Stream stream, bool leaveOpen = true)
 {
     using (var writer = new ObjectBinaryWriter(stream, leaveOpen, Endianness.Little))
         @this.Write(writer);
 }
Esempio n. 9
0
        private IBinarySerializable SerializeAndDeserializeObject(IBinarySerializable obj, Type objType)
        {
            dynamic buffer;

            using (var stream = new MemoryStream())
            {
                var binaryWriter = new BinaryWriter(stream);

                obj.Write(binaryWriter);

                buffer = stream.ToArray();
            }

            var returnObj = Activator.CreateInstance(objType) as IBinarySerializable;

            using (var readStream = new MemoryStream(buffer))
            {
                var binaryReader = new BinaryReader(readStream);
                returnObj.Read(binaryReader);
            }

            return returnObj;
        }