{"id":4972,"date":"2014-03-30T17:19:30","date_gmt":"2014-03-30T17:19:30","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/serialize-inherited-classes-using-protobuf-net-collection-of-common-programming-errors\/"},"modified":"2014-03-30T17:19:30","modified_gmt":"2014-03-30T17:19:30","slug":"serialize-inherited-classes-using-protobuf-net-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/serialize-inherited-classes-using-protobuf-net-collection-of-common-programming-errors\/","title":{"rendered":"Serialize inherited classes using protobuf-net-Collection of common programming errors"},"content":{"rendered":"<p>I have a problem serializing derived classes using protobuf-net. I don&#8217;t know if it is because it is not supported, or I am doing something wrong.<\/p>\n<p>I have a generic base class (which I can serialize directly) and then i make a specialization of this, but this one I can&#8217;t serialize. The following is the code for the two classes and a example of usage. Am i doing something wrong?<\/p>\n<p><strong>Edit<\/strong><\/p>\n<p>Added restriction to the generic types<\/p>\n<p><strong>Base<\/strong><\/p>\n<pre><code>[ProtoBuf.ProtoContract]\npublic class Base\n    where TKey : Key\n    where TValue : Key\n{\n    [ProtoBuf.ProtoMember(1)]\n    public Dictionary Data { get; set; }\n    [ProtoBuf.ProtoMember(2)]\n    public TValue DefaultValue { get; set; }\n\n    public Base()\n    {\n        this.Data = new Dictionary();\n    }\n\n    public Base(TValue defaultValue)\n        : this()\n    {\n        this.DefaultValue = defaultValue;\n    }\n\n    public TValue this[TKey x, TKey y]\n    {\n        get\n        {\n            try { return this.Data[x][y]; }\n            catch { return this.DefaultValue; }\n        }\n        set\n        {\n            if (!this.Data.ContainsKey(x))\n                this.Data.Add(x, new Dictionary { { y, value } });\n            else\n                this.Data[x][y] = value;\n        }\n    }\n}\n<\/code><\/pre>\n<p><strong>Key class<\/strong><\/p>\n<pre><code>public abstract class Key\n{\n}\n<\/code><\/pre>\n<p><strong>Specialized key<\/strong><\/p>\n<pre><code>[ProtoBuf.ProtoContract]\npublic class IntKey : Key\n{\n    [ProtoBuf.ProtoMember(1)]\n    public int Value { get; set; }\n\n    public IntKey() { }\n\n    public override int GetHashCode()\n    {\n        return this.Value.GetHashCode();\n    }\n\n    public override bool Equals(object obj)\n    {\n        if (ReferenceEquals(obj, null)) return false;\n        if (ReferenceEquals(obj, this)) return true;\n\n        var s = obj as IntKey;\n        return this.Value.Equals(s.Value);\n    }\n}\n<\/code><\/pre>\n<p><strong>Specialized class<\/strong><\/p>\n<pre><code>[ProtoBuf.ProtoContract]\npublic class IntWrapper : Base\n    where TKey : Key\n{\n    public IntWrapper()\n        : base() { }\n\n    public IntWrapper(IntKey defaultValue)\n        : base(defaultValue) { }\n}\n<\/code><\/pre>\n<p><strong>An example of usage<\/strong><\/p>\n<pre><code>var path = @\"C:\\Temp\\data.dat\";\nvar data = new IntWrapper(new IntKey { Value = 0 }); \/\/ This will not be serialized\nfor(var x = 0; x &lt; 10; x++)\n{\n    for(var y = 0; y &lt; 10; y++)\n        data[new IntKey { Value = x }, new IntKey { Value = y }] = new IntKey { Value = x + y };\n}\n\nusing (var fileStream = new FileStream(path, FileMode.Create))\n{\n    ProtoBuf.Serializer.Serialize(fileStream, data);\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have a problem serializing derived classes using protobuf-net. I don&#8217;t know if it is because it is not supported, or I am doing something wrong. I have a generic base class (which I can serialize directly) and then i make a specialization of this, but this one I can&#8217;t serialize. The following is the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4972","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4972","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4972"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4972\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}