ProtoBuf.net基类的属性不包括当序列化派生类 [英] ProtoBuf.net Base class properties is not included when serializing derived class

查看:483
本文介绍了ProtoBuf.net基类的属性不包括当序列化派生类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

采用最新的2.0测试版ProtoBuf.net的版本我想序列化派生类(只是举例),我得到空文件。为什么基类的属性不是序列?

  [ProtoContract]
[可序列化]
公共类Web2PdfClient:Web2PdfEntity
{

}

[ProtoContract]
[可序列化]
公共类Web2PdfEntity:EngineEntity
{

    [ProtoMember(1)]
    公共字符串名称{获取;组; }
    [ProtoMember(2)]
    公共字符串卷曲{获得;组; }
    [ProtoMember(3)]
    公共字符串文件名{获得;组; }

}


[ProtoContract]
[可序列化]
公共类EngineEntity
{

    公共BOOL结果{获得;组; }
    公共字符串的ErrorMessage {获得;组; }
    公共BOOL IsMembershipActive {获得;组; }
    公众诠释ConversionTimeout {获得;组; }
    公共字节[] FileStorage {获得;组; }
}
 

在使用低于code,我得到空文件序列化类。

  VAR Web2PDF =新Web2PdfClient
                          {
                              卷曲=htt​​p://www.google.com
                              文件名=的test.txt
                          };
        使用(var文件= File.Create(@C:\用户\管理\项目\ TEMP \ TEST.bin,烧写))
        {
            Serializer.Serialize(文件,Web2PDF);

        }
 

解决方案

其实,我就是没有抛出异常很惊讶 - 我会调查!为了使该工作,基础类型必须有一种独特的方式来表示每个子类型。这可以通过属性被指定,或(在v2)的运行时。例如:

  [ProtoContract]
[可序列化]
公共类Web2PdfClient:Web2PdfEntity
{

}

[ProtoContract]
[ProtoInclude(7的typeof(Web2PdfClient))]
[可序列化]
公共类Web2PdfEntity:EngineEntity
{...}
 

有没有什么特别的 7 只是它不应该与该类型定义的任何其他成员发生冲突。多个亚型可以定义(具有不同的标签)。还要注意的是protobuf网不看 [Serializable接口] ,所以你不需要,除非你使用了的BinaryFormatter (或类似)。

同样, EngineEntity 应该宣传的及其的预期亚型,并应注明成员序列化(以及针对其标签)。

Using latest 2.0 beta version of ProtoBuf.net I am trying to serialize derived class(just example) and I get empty file. Why base class properties is not serialized?

[ProtoContract]
[Serializable]
public class Web2PdfClient : Web2PdfEntity
{

}

[ProtoContract]
[Serializable]
public class Web2PdfEntity : EngineEntity
{

    [ProtoMember(1)]
    public string Title { get; set; }
    [ProtoMember(2)]
    public string CUrl { get; set; }
    [ProtoMember(3)]
    public string FileName { get; set; }

}


[ProtoContract]
[Serializable]
public class EngineEntity
{

    public bool Result { get; set; }
    public string ErrorMessage { get; set; }
    public bool IsMembershipActive { get; set; }
    public int ConversionTimeout { get; set; }
    public byte[] FileStorage { get; set; }
}

While using code below to serialize class I get empty file.

var Web2PDF = new Web2PdfClient
                          {                                
                              CUrl = "http://www.google.com",
                              FileName = "test.txt"
                          };
        using (var file = File.Create(@"C:\Users\Administrator\Projects\temp\test.bin"))
        {
            Serializer.Serialize(file, Web2PDF);

        }

解决方案

Actually, I'm quite surprised that didn't throw an exception - I will investigate! In order for that to work, the base-type must have a unique way to indicate each of the sub-types. This can be specified via attributes, or (in v2) at runtime. For example:

[ProtoContract]
[Serializable]
public class Web2PdfClient : Web2PdfEntity
{

}

[ProtoContract]
[ProtoInclude(7, typeof(Web2PdfClient))]
[Serializable]
public class Web2PdfEntity : EngineEntity
{ ... }

There's nothing special about 7 except that it shouldn't collide with any other members defined for that type. Multiple subtypes can be defined (with different tags). Note also that protobuf-net doesn't look at [Serializable], so you don't need that unless you are also using BinaryFormatter (or similar).

Similarly, EngineEntity should advertise its expected subtypes, and should indicate the members to serialize (and against which tag).

这篇关于ProtoBuf.net基类的属性不包括当序列化派生类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆