Deveel.Data.Serialization.BinarySerializer.Serialize C# (CSharp) Method

Serialize() public method

public Serialize ( BinaryWriter writer, object obj ) : void
writer BinaryWriter
obj object
return void
		public void Serialize(BinaryWriter writer, object obj) {
			if (writer == null)
				throw new ArgumentNullException("writer");
			if (obj == null)
				throw new ArgumentNullException("obj");

			var objType = obj.GetType();

#if !PCL
			if (!Attribute.IsDefined(objType, typeof(SerializableAttribute)))
#else
			if (!objType.GetTypeInfo().IsDefined(typeof(SerializableAttribute)))
#endif
				throw new ArgumentException(String.Format("The type '{0} is not serializable", objType.FullName));

			var graph = new SerializationInfo(objType, new FormatterConverter());
			var context = new StreamingContext();

#if PCL
			if (objType.IsTypeOf(typeof(ISerializable))) {
#else
			if (typeof (ISerializable).IsAssignableFrom(objType)) {
#endif
				((ISerializable) obj).GetObjectData(graph, context);
			} else {
				GetObjectValues(objType, obj, graph);
			}

			SerializeGraph(writer, Encoding, objType, graph);
		}

Same methods

BinarySerializer::Serialize ( Stream stream, object obj ) : void

Usage Example

        public static void SerializeObjectNameWithNoParent()
        {
            var objName = new ObjectName("name");

            var serializer = new BinarySerializer();
            byte[] bytes;

            using (var memoryStream = new MemoryStream()) {
                serializer.Serialize(memoryStream, objName);
                memoryStream.Flush();
                bytes = memoryStream.ToArray();
            }

            object graph = null;
            using (var memoryStream = new MemoryStream(bytes)) {
                graph = serializer.Deserialize(memoryStream, typeof(ObjectName));
            }

            Assert.IsNotNull(graph);
            Assert.IsInstanceOf<ObjectName>(graph);

            var objName2 = (ObjectName) graph;
            Assert.AreEqual(objName.Name, objName2.Name);
            Assert.AreEqual(objName, objName2);
        }
All Usage Examples Of Deveel.Data.Serialization.BinarySerializer::Serialize