Click here to Skip to main content
15,897,273 members
Articles / Desktop Programming / MFC

Implementing MFC-Style Serialization in .NET - Part 1

Rate me:
Please Sign up or sign in to vote.
4.50/5 (9 votes)
21 Jan 2009CPOL7 min read 50.1K   446   23  
This article shows how to implement MFC-style object serialization in .NET.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ArchiveSerialization;

namespace ArchiveTest
{
	class Program
	{
		static void Main(string[] args)
		{
			Console.WriteLine("Boolean " + sizeof(bool));
			Console.WriteLine("Double " + sizeof(double));
			Console.WriteLine("Int " + sizeof(int));
			Console.WriteLine("Long " + sizeof(long));
			Console.WriteLine("Char " + sizeof(Char));
			Console.WriteLine();
			
			Person parent = new Person();
			parent.Name = "Joe Smith";
			parent.Age = 24;
			parent.Sex = 'M';
			parent.Weight = 123.45;
			parent.Height = 1000000.0F;
			parent.Birthday = DateTime.Now;
			parent.Deceased = true;
			Console.WriteLine("Parent GUID is {0}", parent.guid.ToString());


			Person child1 = new Person();
			parent.Children.Add(child1);
			child1.Name = "Sallry Smith";
			child1.Age = 2;
			child1.Sex = 'F';
			child1.Weight = 75.85;
			child1.Height = 18;
			child1.Birthday = DateTime.Now;
			child1.Deceased = false;
			Console.WriteLine("Child 1 GUID is {0}", child1.guid.ToString());

			child1 = new Person();
			parent.Children.Add(child1);
			child1.Name = "Farry Smith";
			child1.Age = 3;
			child1.Sex = 'F';
			child1.Weight = 22.85;
			child1.Height = 4.5F;
			child1.Birthday = DateTime.Now;
			child1.Deceased = false;
			Console.WriteLine("Child 2 GUID is {0}", child1.guid.ToString());
			
			FileStream fsOut = File.Create("out.bin");
			Archive ar = new Archive(fsOut, ArchiveOp.store);
			parent.Serialize(ar);
			fsOut.Flush();
			fsOut.Close();

			Person personIn = new Person();

			FileStream fsIn = File.Open("out.bin", FileMode.Open);
			Archive arIn = new Archive(fsIn, ArchiveOp.load);
			personIn.Serialize(arIn);
			fsIn.Close();

			personIn.WriteToConsole();

			MemoryStreamExample(parent);

			Console.ReadLine();
		}


		static public void MemoryStreamExample(Person parent)
		{
			Console.WriteLine("");
			Console.WriteLine("---------------------");
			Console.WriteLine("Memory Stream Example");

			MemoryStream msOut = new MemoryStream();
			Archive ar = new Archive(msOut, ArchiveOp.store);
			parent.Serialize(ar);
			msOut.Flush();

			byte[] bytes = msOut.GetBuffer();
			msOut.Close();

			MemoryStream msIn = new MemoryStream(bytes);

			Person personIn = new Person();

			Archive arIn = new Archive(msIn, ArchiveOp.load);
			personIn.Serialize(arIn);
			msIn.Close();

			personIn.WriteToConsole();


		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
President Starpoint Software Inc.
United States United States
Bob Pittenger is founder and President of Starpoint Software Inc. He holds a B.A. degree from Miami University, M.S. and Ph.D. degrees from Purdue University, and an MBA from Xavier University. He has been programming since 1993, starting with Windows application development in C++/MFC and moving to C# and .NET around 2005 and is a .NET Microsoft Certified Professional Developer.

Bob is the author of two books:
Billionaire: How the Ultra-Rich Built Their Fortunes Through Good and Evil and What You Can Learn from Them
and
Wealthonomics: The Most Important Economic and Financial Concepts that Can Make You Rich Fast.
Visit http://www.billionairebook.net for more information.

Comments and Discussions