Joslian / protobuf-net

Automatically exported from code.google.com/p/protobuf-net
Other
0 stars 0 forks source link

sub-classes serialization #455

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Please include an e-mail address if this might need a dialogue!
Derek.Streyt.aw@gmai.rom
==============

What steps will reproduce the problem?
1. I created 2 classes

[ProtoContract(SkipConstructor = true)]
public class BuildConstruct : TimingBase
{
    [ProtoMember(1)]
    public string name;

    public BuildConstruct():base(1)  { }

    protected override void Complete()
    {
    }
}

and 

[ProtoContract(SkipConstructor = true)]
public class BuildingWithWorkers : TimingBase
{
    [ProtoMember(1)]
    public GameResources BuildResources { get; protected set; }

    public List<Worker> Workers { get; protected set; }

    [ProtoMember(2)]
    public List<int> WorkerIds { get; protected set; }

    public BuildingWithWorkers(float time, List<Worker> workers = null,  GameResources resources = null) : base(time)
    {
        if (workers != null)
        {
            Workers = new List<Worker>(workers);
            WorkerIds = new List<int>();
            foreach (Worker worker in workers)
            {
                WorkerIds.Add(worker.CurrentUnit.Id);
            }
        }

        BuildResources = resources;
    }

    public override void OnRemove(Unit unit)
    {
        base.OnRemove(unit);
        foreach (Worker worker in Workers)
        {
            worker.CurrentWork = null;
        }
    }

    public virtual void Cancel(MacroGameEngine engine)
    {
        engine.Resources = engine.Resources += BuildResources;
        CurrentUnit.RemoveStrategy(this);
    }

    protected override void Complete()
    {

    }
}

base class UnitStrategyBase

    [
    ProtoContract(),
    ProtoInclude(2001, typeof(ArmyFarm)),
    ProtoInclude(2002, typeof(BuildingBase)),
    ProtoInclude(2003, typeof(BuildingHealth)),
    ProtoInclude(2004, typeof(BuildingWithWorkers)), /// !!!
    ProtoInclude(2005, typeof(Construction)),
    ProtoInclude(2006, typeof(FarmBase)),
    ProtoInclude(2007, typeof(HealthBase)),
    ProtoInclude(2008, typeof(Repair)),
    ProtoInclude(2009, typeof(SyncBase)),
    ProtoInclude(2010, typeof(TimingBase)),
    ProtoInclude(2011, typeof(UnitHealth)),
    ProtoInclude(2012, typeof(Worker)),
    ProtoInclude(2013, typeof(WorkerFarm)),
    ProtoInclude(2014, typeof(BuildConstruct)) // !!!

    ]
    public abstract class UnitStrategyBase 
    {
...
}

2. Serialize first - InvalidOperationException: Unexpected 
sub-type:InvalidOperationException: Unexpected sub-type: BuildConstruct ? why, 
i set this in protoinclude.

3. Serialize second - ok

protobuf-net r668  win 8.1

Original issue reported on code.google.com by derek.st...@gmail.com on 24 Mar 2015 at 8:39