如何将派生 class 序列化为 Eyeshot 专有文件格式

How to serialize derived class into Eyeshot proprietary file format

我正在尝试将自定义 EntityData class 序列化为 Eyeshot 专有文件格式。关于这个有一篇很棒的文章 (https://devdept.zendesk.com/hc/en-us/articles/360003318873-Eyeshot-Proprietary-File-Format),
如果我序列化基 class,它工作正常,但是我无法序列化从我的基 class 派生的 class。这是一个示例,我尽量保持它的小(请一路阅读评论):

public class BaseClass
{
    public int Id { get; set; }
    public virtual BaseClassSurrogate ConvertToSurrogate() { return new BaseClassSurrogate(this); }
}
public class BaseClassSurrogate : Surrogate<BaseClass>
{
    public BaseClassSurrogate(BaseClass myBaseClass) : base(myBaseClass) { }
    public int Id { get; set; }
    protected override BaseClass ConvertToObject()
    {
        var baseClass = new BaseClass();
        CopyDataToObject(baseClass);
        return baseClass;
    }
    protected override void CopyDataFromObject(BaseClass obj) { Id = obj.Id; }
    protected override void CopyDataToObject(BaseClass obj) { obj.Id = this.Id; }
    public static implicit operator BaseClass(BaseClassSurrogate surrogate) { return surrogate?.ConvertToObject(); }
    public static implicit operator BaseClassSurrogate(BaseClass source) { return source?.ConvertToSurrogate(); }
}

我派生的 class 及其代理实现:

public class DerivedClass : BaseClass
{
    public int Number { get; set; }
    public override BaseClassSurrogate ConvertToSurrogate() { return new DerivedClassSurrogate(this); }
}

public class DerivedClassSurrogate : BaseClassSurrogate
{
    public DerivedClassSurrogate(DerivedClass baseClass) : base(baseClass) { }
    public int Number { get; set; }
    protected override BaseClass ConvertToObject()
    {
        var derivedClass= new DerivedClass();
        CopyDataToObject(derivedClass);
        return derivedClass;
    }
    protected override void CopyDataFromObject(BaseClass obj)
    {
        if (obj is DerivedClass derivedClass)
            Number = derivedClass.Number;
        base.CopyDataFromObject(obj);
    }
    protected override void CopyDataToObject(BaseClass obj)
    {
        if (obj is DerivedClass derivedClass)
            derivedClass.Number = Number;
        base.CopyDataToObject(obj);
    }
    //I don't understand do I need to call these in derived class as well?
    //public static implicit operator BaseClass(BaseClassSurrogate surrogate) { return surrogate?.ConvertToObject(); }
    //public static implicit operator BaseClassSurrogate(BaseClass source) { return source?.ConvertToSurrogate(); }
}

这是 FillModel 来自 FileSerializer class 的方法:

protected override void FillModel()
{
    base.FillModel();

    Model.Add(typeof(BaseClass), false)
         .SetSurrogate(typeof(BaseClassSurrogate));

    MetaType mt1 = Model[typeof(BaseClassSurrogate)]
        .Add(1, "Id");

    mt1.SetCallbacks(null, null, "BeforeDeserialize", null); 
    mt1.UseConstructor = false; 

    Model.Add(typeof(DerivedClass), false)
    .SetSurrogate(typeof(DerivedClassSurrogate));

    MetaType mt2 = Model[typeof(DerivedClassSurrogate)]
        .Add(1, "Number");

    mt2.SetCallbacks(null, null, "BeforeDeserialize", null); 
    mt2.UseConstructor = false;
}

此代码给我错误:“找不到合适的代理转换运算符 DerivedClass/DerivedClassSurrogate”。任何帮助将不胜感激。

FillModel() 方法中,您忘记为自定义 类 指定层次结构,请尝试以下方式:

protected override void FillModel()
{
    base.FillModel();

    Model.Add(typeof(BaseClass), false)
        .AddSubType(1001, typeof(DerivedClass))
        .SetSurrogate(typeof(BaseClassSurrogate));
    
    Model[typeof(BaseClassSurrogate)]
        .AddSubType(1001, typeof(DerivedClassSurrogate))
        .Add(1, "Id")
        .SetCallbacks(null, null, "BeforeDeserialize", null)
        .UseConstructor = false; 
    
    Model[typeof(DerivedClassSurrogate)]
        .Add(1, "Number")
        .UseConstructor = false;
}