开发者

Protobuf-net serializing based on interfaces

开发者 https://www.devze.com 2023-01-16 08:30 出处:网络
I am using protobuf-net r282 and when I call Serialize I get the InvalidOperationException error message \"Only data-contract classes (and lists/arrays of such) can be processed (error processing Obje

I am using protobuf-net r282 and when I call Serialize I get the InvalidOperationException error message "Only data-contract classes (and lists/arrays of such) can be processed (error processing Object)". At the point where protobuf-net Serializer.Serialize is being called the object in question has been cast to an interface. Is there a way to get around this?

Here's the code:

using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using ProtoBuf;
using System;

namespace EventStore.Core
{
public interface Message
{
}

public interface Event : Message
{
    Guid Id { get; set; }

    int Version { get; set; }
}

[DataContract]
public class InventoryItemCreated : Event
{
    [DataMember(Order = 1)]
    public Guid Id { get; set; }

    [DataMember(Order = 2)]
    public int Version { get; set; }

    [DataMember(Order = 3)]
    public string Name { get; set; }

    public InventoryItemCreated(Guid id, string name)
    {
        Id = id;
        Name = name;
    }

    public InventoryItemCreated()
    {

    }
}

public class DefaultSerializer
{
    private readonly IFormatter formatter = new BinaryFormatter();

    public byte[] Serialize<T>(T graph) where T : class
    {
        if (default(T) == graph)
            return null;

        using (var stream = new MemoryStream())
        {
            this.Serialize(graph, stream);
            return stream.ToArray();
        }
    }

    public virtual void Serialize<T>(T graph, Stream output) where T : class
    {
        this.formatter.Serialize(output, graph);
    }

    public T Deserialize<T>(byte[] serialized) where T : class
    {
        if (null == serialized || 0 == serialized.Length)
            return default(T);

        using (var stream = new MemoryStream(serialized))
            return this.Deserialize<T>(stream);
    }

    public virtual T Deserialize<T>(Stream input) where T : class
    {
        return (T)this.formatter.Deserialize(input);
    }
}

public class ProtoBufSerializer : DefaultSerializer
{
    public override void Serialize<T>(T graph, Stream output)
    {
        Serializer.Serialize<T>(output, graph);
    }

    public override T Deserialize<T>(Stream input)
    {
        return Serializer.Deserialize<T>(input);
    }
}

class Program
{
    static void Main(string[] args)
    {
        ProtoBufSerializer serializer = new ProtoBufSerializer();
        InventoryItemCreated item = new InventoryItemCreated(Guid.NewGuid(), "Widget");
        byte[] buffer = serializer.S开发者_如何学Pythonerialize((Message)item);
    }
}

}


This is something that I have been looking at for "v2", but it is incomplete. I have been sent a patch for v1 that does this and which I can share, but I haven't rigorously tested this. Let me know if you want this patch file.

0

精彩评论

暂无评论...
验证码 换一张
取 消