I have this class:
using Sierra.Collections;
namespace Sierra
{
public sealed class Worlds : SafeKeyedCollection<byte, World>
{
public Worlds() : base() { }
protected override byte GetKeyForItem(World item)
{
return item.Id;
}
}
}
World class:
using Sierra.Collections;
namespace Sierra
{
public sealed class World : SafeKeyedCollection<byte, Channel>
{
public byte Id { get; set; }
public string Name { get; set; }
public ushort Port { get; set; }
public ushort ShopPort { get; set; }
public ushort MonsterLifePort { get; set; }
public ushort TalkPort { get; set; }
public byte Ribbon { get; set; }
public byte Channels { get; set; }
public string EventMessage { get; set; }
public string ScrollingHeader { get; set; }
public bool AllowMultiLeveling { get; set; }
public int DefaultCreationSlots { get; set; }
public bool DisableCharacterCreation { get; set; }
public World() : base() { }
protected override byte GetKeyForItem(Channel item)
{
return item.Id;
}
}
}
When I a Worlds
object, it doesn't serialize it. The list appears to be empty:
public Worlds Worlds { get; set; }
It outputs:
"Worlds": [
[]
]
Why's that? Can't you serialize a class that inherits from another class?
SafeKeyedCollection
using System;
using System.Collections;
using System.Collections.Generic;
namespace Sierra.Collections
{
/// <summary>
/// Thread safe NumericalKeyedCollection class - Patel
/// Soooooooooooooo fire
/// </summary>
public abstract class SafeKeyedCollection<TKey, TItem> : IEnumerable<TItem>
{
private Object m_Lock;
private Dictionary<TKey, TItem> m_Inner;
public SafeKeyedCollection()
{
m_Lock = new object();
m_Inner = new Dictionary<TKey, TItem>();
}
public int Count
{
get
{
lock (m_Lock)
{
return m_Inner.Count;
}
}
}
public TItem this[TKey key]
{
get
{
lock (m_Lock)
{
TItem ret;
m_Inner.TryGetValue(key, out ret);
return ret;
}
}
}
public void Add(TItem item)
{
this.InsertItem(item);
}
public void Remove(TItem item)
{
this.RemoveItem(item);
}
protected virtual void InsertItem(TItem item)
{
lock (m_Lock)
{
TKey key = GetKeyForItem(item);
m_Inner.Add(key, item);
}
}
protected virtual void RemoveItem(TItem item)
{
lock (m_Lock)
{
TKey key = GetKeyForItem(item);
m_Inner.Remove(key);
}
}
public void Clear()
{
lock (m_Lock)
{
m_Inner.Clear();
}
}
public bool Contains(TKey key)
{
lock (m_Lock)
{
return m_Inner.ContainsKey(key);
}
}
public bool Contains(TItem value)
{
lock (m_Lock)
{
return m_Inner.ContainsValue(value);
}
}
protected abstract TKey GetKeyForItem(TItem item);
protected virtual void ClearItems() { }
public IEnumerator<TItem> GetEnumerator()
{
return new SafeEnumerator<TItem>(() => m_Inner.Values.GetEnumerator(), m_Lock);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
Thanks