Click here to Skip to main content
15,895,799 members
Articles / Programming Languages / C++/CLI

Serialization Primer - MC++

Rate me:
Please Sign up or sign in to vote.
4.81/5 (17 votes)
25 May 20023 min read 177.2K   2.6K   31  
A basic introduction to serialization using Managed C++
#include "stdafx.h"

#using <mscorlib.dll>

using namespace System;
using namespace System::Runtime::Serialization;
using namespace System::Runtime::Serialization::Formatters::Binary;
using namespace System::IO;

[Serializable]
__gc class CData : public ISerializable 
{
private:
	String* m_name;
	Int32 m_age;
	String* m_dontsave;
public:
	__property String* get_Name()
    { 
		return m_name; 
	}
	__property void set_Name(String* s)
    { 
		m_name = s; 
	}
	__property Int32 get_Age()
    { 
		return m_age; 
	}
	__property void set_Age(Int32 i)
    { 
		m_age = i; 
	}
	__property String* get_DontSave()
    { 
		return m_dontsave; 
	}
	__property void set_DontSave(String* s)
    { 
		m_dontsave = s; 
	}
	void GetObjectData(SerializationInfo *si, StreamingContext sc)
	{
		si->AddValue("m_name",m_name);
		si->AddValue("m_age",m_age);
	}	
	CData()
	{
		m_name="Default";
		m_dontsave = "Default";
	}
protected:
	CData(SerializationInfo *si, StreamingContext sc)
	{
		m_name = si->GetString("m_name");
		m_age = si->GetInt32("m_age");
	}
};

int wmain(int argc, char **argv)
{
	if(argc==1)
	{
		Console::WriteLine("Usage:- Serialize01 [L/S]");
		return 0;
	}
	if(argv[1][0]=='S')
	{
		Console::WriteLine("Saving to file");
		CData *data = new CData();
		data->Name = "Johnny Bravo";
		data->Age = 24;
		data->DontSave = "Hello World";
		FileStream *fs = new FileStream("data.txt" , 
			FileMode::Create, FileAccess::ReadWrite); 
		BinaryFormatter *bf = new BinaryFormatter();	
		bf->Serialize(fs,data);
		fs->Close();
	}
	else if(argv[1][0]=='L')
	{
		Console::WriteLine("Loading from file");
		CData *data;
		FileStream *fs = new FileStream("data.txt" , 
			FileMode::Open, FileAccess::ReadWrite); 
		BinaryFormatter *bf = new BinaryFormatter();	
		data = (CData*) bf->Deserialize(fs);
		Console::WriteLine("data->Name is {0}",data->Name);
		Console::WriteLine("data->Age is {0}",data->Age.ToString());
		Console::WriteLine("data->DontSave is {0}",data->DontSave);
		fs->Close();
	}
	else
	{
		Console::WriteLine("Unknown option");
	}
    return 0;
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.

Nish authored C++/CLI in Action for Manning Publications in 2005, and co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.

Nish's Technology Blog : voidnish.wordpress.com

Comments and Discussions