public Serialize ( Stream serializationStream, object graph ) : void | ||
serializationStream | Stream | |
graph | object | |
return | void |
using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary; [Serializable] public class Person { public string Name { get; set; } public int Age { get; set; } } class Program { static void Main(string[] args) { Person p = new Person { Name = "John Doe", Age = 25 }; BinaryFormatter formatter = new BinaryFormatter(); using (MemoryStream stream = new MemoryStream()) { formatter.Serialize(stream, p); byte[] data = stream.ToArray(); Console.WriteLine(BitConverter.ToString(data)); } } }
using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary; [Serializable] public class Cat { public string Name { get; set; } public int Age { get; set; } public string Color { get; set; } } class Program { static void Main(string[] args) { Cat c = new Cat { Name = "Fluffy", Age = 3, Color = "white" }; BinaryFormatter formatter = new BinaryFormatter(); using (FileStream stream = File.Create("cats.dat")) { formatter.Serialize(stream, c); } } }In this example, we define a Cat class with three properties. We create an instance of the Cat class and use a BinaryFormatter object to serialize it into a file named "cats.dat". The package library for the System.Runtime.Serialization.Formatters.Binary namespace is System.Runtime.Serialization.Formatters.dll. This library is included in the .NET Framework and is automatically referenced by Visual Studio projects.
public Serialize ( Stream serializationStream, object graph ) : void | ||
serializationStream | Stream | |
graph | object | |
return | void |