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.
Deserialize code
We first deserialize the number of items, create a new list and add to it each item we deserialize.
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.
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:
writer.Write(list.Count);
for (int i = 0 ; i < list.Count ; i++) {
writer.Write((int)list[i]);
}
We first deserialize the number of items, create a new list and add to it each item we deserialize.
PHP:
int count = reader.ReadInt();
list = new List<YourTypeHere>(count);
for (int i = 0 ; i < count ; i++) {
list.Add((YourTypeHere)reader.ReadYourTypeHere());
}
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:
using System;
using System.Collections.Generic;
using System.Text;
namespace Server.Items.Runes {
public class RunicItem : Item, IRunic {
#region Constructors
public RunicItem (int itemID) : base(itemID) {
runes = new List<RuneType>();
}
public RunicItem (Serial serial) : base(serial) {
}
#endregion
#region IRunic Members
private List<RuneType> runes;
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 = 0 ; 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 = 0 ; i < count ; i++) {
runes.Add((RuneType)reader.ReadInt());
}
}
#endregion
}
}