7

I have a List<object> with different types of objects in it like integers, strings, and custom types. All custom types are protobuf-adjusted. What I wanna do now is to serialize / deserialize this list with protobuf.net. Up until now I suspect that I have to declare each and every type explicitly, which is unfortunately not possible with these mixed-list constructs. Because the binary formater has no problems to do these things I hope that I missed something and that you can help me out. So my question is how to deal with objects in protobuf.net.


Option C would be to parse access logs from the web server. No extra storage needed, all sorts of extra information is stored, and even requests to images and JavaScript files are stored.

..

However, if you just want to track visits to pages where you run your own code, I'd definitely go for Option A, unless you're expecting extreme amounts of traffic on your site.

That way you can create overviews per hour of the day, and store more information than just the timestamp (like the visited page, the user's browser, etc.). You might not need that now, but later on you might thank yourself for not losing that information. If at some point the table grows too large, you can always think of ways on how to deal with that.

4

2 に答える 2

13

(開示:私はprotobuf-netの作者です)

BinaryFormatterメタデータベースのシリアライザーです。つまり、シリアル化されたすべてのオブジェクトに関する.NETタイプ情報を送信します。protobuf-netは、コントラクトベースのシリアライザーです(XmlSerializer/と同等のバイナリDataContractSerializerであり、これも拒否されます)。

もう一方の端には何を送信しているかを知る方法がないため、任意のオブジェクトを転送するための現在のメカニズムはありません。ただし、送信するさまざまなオブジェクトタイプの既知のセットがある場合は、オプションがある場合があります。パイプラインには、(ビルド時に修正される属性だけでなく)ランタイム拡張可能なスキーマを許可する作業もありますが、これは完全にはほど遠いものです。


これは理想的ではありませんが、機能します...ランタイムスキーマをサポートするための作業を完了すると、より簡単になるはずです。

using System;
using System.Collections.Generic;
using ProtoBuf;
[ProtoContract]
[ProtoInclude(10, typeof(DataItem<int>))]
[ProtoInclude(11, typeof(DataItem<string>))]
[ProtoInclude(12, typeof(DataItem<DateTime>))]
[ProtoInclude(13, typeof(DataItem<Foo>))]
abstract class DataItem {
    public static DataItem<T> Create<T>(T value) {
        return new DataItem<T>(value);
    }
    public object Value {
        get { return ValueImpl; }
        set { ValueImpl = value; }
    }
    protected abstract object ValueImpl {get;set;}
    protected DataItem() { }
}
[ProtoContract]
sealed class DataItem<T> : DataItem {
    public DataItem() { }
    public DataItem(T value) { Value = value; }
    [ProtoMember(1)]
    public new T Value { get; set; }
    protected override object ValueImpl {
        get { return Value; }
        set { Value = (T)value; }
    }
}
[ProtoContract]
public class Foo {
    [ProtoMember(1)]
    public string Bar { get; set; }
    public override string ToString() {
        return "Foo with Bar=" + Bar;
    }
}
static class Program {
    static void Main() {
        var items = new List<DataItem>();
        items.Add(DataItem.Create(12345));
        items.Add(DataItem.Create(DateTime.Today));
        items.Add(DataItem.Create("abcde"));
        items.Add(DataItem.Create(new Foo { Bar = "Marc" }));
        items.Add(DataItem.Create(67890));

        // serialize and deserialize
        var clone = Serializer.DeepClone(items);
        foreach (DataItem item in clone) {
            Console.WriteLine(item.Value);
        }
    }
}
于 2009-05-29T05:06:58.647 に答える
0
List<YourClass> list;
ProtoBuf.Serializer.Deserialize<List<YourClass>>(filestream);
于 2015-06-02T10:12:53.700 に答える