English 中文(简体)
DataContract serialization with protobuf-net r275
原标题:

I just updated to r275 version and it doesn t seem to manage correctly DataContract classes any more By serializing this very simple class:

[DataContract]
public class ProtoData
{
    [DataMember(Order = 1)]
    private long _id;
    [DataMember(Order = 2)]
    private string _firstName;
    [DataMember(Order = 3)]
    private string _lastName;

    public long Id
    {
        get { return _id; }
        set { _id = value; }
    }

    public string FirstName
    {
        get { return _firstName; }
        set { _firstName = value; }
    }

    public string LastName
    {
        get { return _lastName; }
        set { _lastName = value; }
    }

    public ProtoData(long id, string firstName, string lastName)
    {
        _id = id;
        _firstName = firstName;
        _lastName = lastName;
    }

    public ProtoData()
    {
    }

I get Only data-contract classes (and lists/arrays of such) can be processed (error processing ProtoData)

最佳回答

Really? that is.... odd; I would have expected the unit tests to spt such a breaking change. Are you sure you are using the right version? There is a 2.0 version (which doesn t include [DataContract] support, since this is in WCF, a 3.0 extension) and a separate 3.0 version. You want the 3.0 version (NET30.zip).

Tested successfully with r275/NET30:

static void Main() {
    ProtoData pd = new ProtoData {
        FirstName = "Marc",
        LastName = "Gravell",
        Id = 23354
    }, clone;
    using (MemoryStream ms = new MemoryStream()) {
        Serializer.Serialize(ms, pd);
        Console.WriteLine(ms.Length);
        ms.Position = 0;
        clone = Serializer.Deserialize<ProtoData>(ms);            
    }
    Console.WriteLine(clone.FirstName);
    Console.WriteLine(clone.LastName);
    Console.WriteLine(clone.Id);
}

With output:

19
Marc
Gravell
23354
问题回答

Try the following:

  • Remove all private members
  • Use public properties

    public string LastName;

  • Mark all public properties with [DataMember]





相关问题
WCF hosting in .NET compact framework

I would like to host a service on a WinCE device. The WinCE device is the host which can be accessed(control and data acquisition) by multiple clients (PC or WinCE) over serial port, TCP, USB etc. I ...

protobuf-net [de]serializing across assembly boundaries

I have a base class in one assembly and a large number of generated classes in another that inherit from the base class. Using protobuf-net (r282) to serialize a list of the base type fails when ...

Mismatched group tags detected in message - protobuf-net

I’m quite new to Silverlight. I’m working for a project which mainly depends on Serialization and Deserialization. Formerly, for WPF I was comfortable with Serializable classes. For silverlight, I ...

Serializing a List of objects using Protobuf-net

I ve been looking to do some binary serialization to file and protobuf-net seems like a well-performing alternative. I m a bit stuck in getting started though. Since I want to decouple the definition ...

DataContract serialization with protobuf-net r275

I just updated to r275 version and it doesn t seem to manage correctly DataContract classes any more By serializing this very simple class: [DataContract] public class ProtoData { [DataMember(...

热门标签