Serialize nested classes : Serializable « File Directory « Visual C++ .NET

Home
Visual C++ .NET
1.2D
2.Class
3.Collections
4.Data Type
5.Database ADO.net
6.Delegate
7.Development
8.File Directory
9.Function
10.Generics
11.GUI Form
12.Language Basics
13.Network
14.Reflection
15.Security
16.Statement
17.Structure
18.Thread
19.XML
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Visual C++ .NET » File Directory » Serializable 
Serialize nested classes
 

#include "stdafx.h"
using namespace System;
using namespace System::IO;
using namespace System::Runtime::Serialization::Formatters::Binary;

[Serializable]
ref class RoleAttr{
public:
    property int Wisdom;

    RoleAttr(int Wis);
    void Print();
};

RoleAttr::RoleAttr(int Wis)
{
    this->Wisdom       = Wis;
}

void RoleAttr::Print()
{
    Console::WriteLine("Wis: {1}", Wisdom);
}

[Serializable]
ref class Role{
public:
    property String ^Name;
    property String ^Race;
    property String ^Class;
    property RoleAttr ^pattr;


    Role (String ^Name, String ^Race, String ^Class,int Wis);
    void Print();
};

Role::Role (String ^Name, String ^Race, String ^Class,int Wis)
{
    this->Name  = Name;
    this->Race  = Race;
    this->Class Class;
    this->pattr = gcnew RoleAttr(Wis);
}

void Role::Print()
{
    Console::WriteLine("Name:  {0}", Name);
    Console::WriteLine("Race:  {0}", Race);
    Console::WriteLine("Class: {0}"Class);
    pattr->Print();
}

void main()
{
    Role ^r = gcnew Role("r""Human""Thief"11);
    r->Print();

    FileStream ^plStream = File::Create("Role.dat");

    BinaryFormatter ^bf = gcnew BinaryFormatter();
    bf->Serialize(plStream, r);
    plStream->Close();

    plStream = File::OpenRead("Role.dat");

    Role ^rClone = (Role^)bf->Deserialize(plStream);
    plStream->Close();

    rClone->Print();
}

   
  
Related examples in the same category
1.Mark class as Serializable
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.