{"id":4816,"date":"2014-03-30T15:34:58","date_gmt":"2014-03-30T15:34:58","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/serialize-objects-with-inheritance-by-defining-serialize-method-only-in-base-class-collection-of-common-programming-errors\/"},"modified":"2014-03-30T15:34:58","modified_gmt":"2014-03-30T15:34:58","slug":"serialize-objects-with-inheritance-by-defining-serialize-method-only-in-base-class-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/serialize-objects-with-inheritance-by-defining-serialize-method-only-in-base-class-collection-of-common-programming-errors\/","title":{"rendered":"Serialize objects with inheritance by defining serialize method only in base class?-Collection of common programming errors"},"content":{"rendered":"<p>I am currently doing a game in C# (for studies purpose). I&#8217;m trying to implement a Load\/Save feature, which consist of saving every data of the game in a file, and the user can be able to reload the game saved after.<\/p>\n<p>In the game I have a nation class attached to each players, and every nation inherit from <code>Nation<\/code>. For the moment, I have only apply the <code>ISerializable<\/code> interface on the base class, <code>Nation<\/code>, because subclasses only have method override and no new attributes to serialize.<\/p>\n<pre><code>[Serializable()]\npublic abstract class Nation : ISerializable \n{\n    public void GetObjectData(SerializationInfo info, StreamingContext context)\n    {\n        \/\/ save attributes\n    }\n\n    public Nation() { } \/\/ default constructor, to be able to do \"new NationDwarf()\" for example.\n\n    \/\/ Deserialization constructor.\n    public Nation(SerializationInfo info, StreamingContext ctxt)\n    {\n        \/\/ retrieve attributes\n    }\n}\n\npublic class NationDwarf : Nation {\n    \/\/ some methods override\n}\npublic class NationViking : Nation {\n    \/\/ some methods override\n}\n<\/code><\/pre>\n<p>The saving seems fine. But when I&#8217;m trying to retrieve data with this method (unserialization), I get an exception (the first player has a NationDwarf):<\/p>\n<blockquote>\n<p>SerializationException<\/p>\n<p>The constructor is missing for deserialize an object of type &#8216;SmallWorld.NationDwarf&#8217;.<\/p>\n<\/blockquote>\n<p>So, my question is : do I need to specify <strong>for each<\/strong> subclass that it&#8217;s serializable and redefine each method (<code>GetObjectData<\/code> and the constructor) ? Is there a way to keep polymorphism and serialize every subclass, and of course be capable to get the correct type at deserialization?<\/p>\n<p>I didn&#8217;t find any solutions, except redefine the serialization method on each subclass, which is a bit heavy&#8230;<\/p>\n<p>Thanks,<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am currently doing a game in C# (for studies purpose). I&#8217;m trying to implement a Load\/Save feature, which consist of saving every data of the game in a file, and the user can be able to reload the game saved after. In the game I have a nation class attached to each players, and [&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-4816","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4816","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=4816"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4816\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}