Go Back   RunUO - Ultima Online Emulation > RunUO > FAQ Forum

FAQ Forum A place to find answers to the most frequently asked questions, and a place to post said answers. Do NOT use this forum to ask questions.

Reply
 
Thread Tools Display Modes
Old 10-15-2008, 05:08 AM   #1 (permalink)
Forum Novice
 
Dereckson's Avatar
 
Join Date: Dec 2005
Location: Belgium
Age: 26
Posts: 261
Send a message via Skype™ to Dereckson
Default How to serialize a List<>

Problem scope

Serialize a System.Collections.Generic.List<T> object

CASE I - T is Mobile, Item or Guild

The RunUO core serialize methods supports those lists:

List<Mobile> yourMobilesList = reader.ReadStrongMobileList();

writer.WriteMobileList(yourMobilesList);

CASE II - T is another type

First, you've to get a simple serializable type from T, like a string, a number or an enum.

Serialize code

We serialize first the number of items in the list, then each list item.

PHP Code:
            writer.Write(list.Count);
            for (
int i < list.Count i++) {
                
writer.Write((int)list[i]);
            } 
Deserialize code
We first deserialize the number of items, create a new list and add to it each item we deserialize.
PHP Code:
            int count reader.ReadInt();
            list = new List<
YourTypeHere>(count);
            for (
int i count i++) {
                list.
Add((YourTypeHere)reader.ReadYourTypeHere());
            } 
Code sample

Here a class RunicItem who allow to define runes for an item. Each rune (identified by an RuneType enum) is put in the runes list.

PHP Code:
using System;
using System.Collections.Generic;
using System.Text;

namespace Server.Items.Runes {
    public class 
RunicItem ItemIRunic {

        
#region Constructors
        
public RunicItem (int itemID) : base(itemID) {
            
runes = new List<RuneType>();
        }

        public 
RunicItem (Serial serial) : base(serial) {
            
        }
        
#endregion

        #region IRunic Members

        
private List<RuneTyperunes;

        public 
RuneType[] Runes {
            
get {
                return 
runes.ToArray();
            }
            
set {
                
runes = new List<RuneType>(value);
            }
        }

        [
CommandProperty(AccessLevel.GameMaster)]
        public 
int CountRunes {
            
get { return runes.Count; }
        }

        public 
void AddRune (RuneType type) {
            
runes.Add(type);
        }

        public 
void DelRune (RuneType type) {
            
runes.Remove(type);
        }

        public 
bool HasRune (RuneType type) {
            return 
runes.Contains(type);
        }

        
#endregion

        #region Sérialisation
        
public override void Serialize (GenericWriter writer) {
            
base.Serialize(writer);
            
writer.Write((int)0); // version
            //IRunic
            
writer.Write(runes.Count);
            for (
int i runes.Count i++) {
                
writer.Write((int)runes[i]);
            }
        }

        public 
override void Deserialize (GenericReader reader) {
            
base.Deserialize(reader);
            
int version reader.ReadInt();
            
//IRunic
            
int count reader.ReadInt();
            
runes = new List<RuneType>(count);
            for (
int i count i++) {
                
runes.Add((RuneType)reader.ReadInt());
            }

        }
        
#endregion
    
}

__________________
La connaissance s'accroît quand on la partage.
Share your knowledge, you'll increase it.

Utopia. Votre Monde.
Dereckson is offline   Reply With Quote
Reply

Bookmarks

Tags
deserialize, list, serialize


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5