{"id":3805,"date":"2014-03-30T05:45:02","date_gmt":"2014-03-30T05:45:02","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/property-marked-with-new-keyword-empty-at-runtime-but-base-property-is-correct-collection-of-common-programming-errors\/"},"modified":"2014-03-30T05:45:02","modified_gmt":"2014-03-30T05:45:02","slug":"property-marked-with-new-keyword-empty-at-runtime-but-base-property-is-correct-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/property-marked-with-new-keyword-empty-at-runtime-but-base-property-is-correct-collection-of-common-programming-errors\/","title":{"rendered":"property marked with new keyword empty at runtime but base property is correct-Collection of common programming errors"},"content":{"rendered":"<p>I have quite a few problems, probably misunderstandings, about the <code>new<\/code> keyword with properties in derived classes. Most of the documentation deals with <code>new<\/code> on derived methods. For instance:<\/p>\n<pre><code>[DataContract]\n[KnownType(typeof(PostCategory))]\npublic class BaseCategory : IEquatable\n{\n\/\/...\n\n    \/\/\/ \n    \/\/\/ Enables access to child categories (immediate ones). Filled in by data access provider. Not serializable.\n    \/\/\/ \n    public SortedDictionary Children { get; set; }\n\/\/...\n}\n\n[DataContract]\n[KnownType(typeof(BaseCategory))]\npublic class PostCategory : BaseCategory\n{\n\/\/...\n    public new SortedDictionary Children { get; set; }\n\/\/...\n}\n<\/code><\/pre>\n<p>Then in my data repository implementation I have a generic implemenation for all the classes that derive from BaseCategory. I do this:<\/p>\n<pre><code>    public class CachedXmlCategorizationProvider : ICategorizationRepository where T : BaseCategory, new()\n    {\n        private readonly ICacheProvider _cacheProvider = AppServices.Cache;\n        private readonly string _file = Path.Combine(XmlProvider.DataStorePhysicalPath, typeof (T).Name, \"categorization.xml\");\n        private SortedDictionary _dictionary;\n\n    \/\/...\n        private SortedDictionary LoadFromDatastore()\n        {\n            if (!DefaultsExist())\n            {\n                return CreateDefaults();\n            }\n\n            var dcs = new DataContractSerializer(typeof (SortedDictionary));\n            XmlDictionaryReader reader =\n                XmlDictionaryReader.CreateTextReader(new FileStream(_file, FileMode.Open, FileAccess.Read),\n                                                     new XmlDictionaryReaderQuotas());\n\n            _dictionary = (SortedDictionary) dcs.ReadObject(reader, true);\n\/* HERE! Apparently this fills in the Children property of the base class *\/\n            \/\/ fill in Children\n            foreach (var pair in _dictionary)\n            {\n                pair.Value.Children = GetChildren(pair.Value.Id);\n            }\n            reader.Close();\n            return _dictionary;\n        }\n    \/\/...\n    }\n<\/code><\/pre>\n<p>and when I try to access the <code>Children<\/code> property of a specific <code>PostCategory<\/code> instance at runtime it is <code>null<\/code> and the <code>base.Children<\/code> is set to the correct dictionary of Children as set by the <code>GetChildren()<\/code> method.<\/p>\n<p>The thing is, my code relies on checking the PostCategory.Children instance and because it&#8217;s null my code is failing.<\/p>\n<p>What is the correct way to achieve what I want (have one base class and change the type of a property in derived class)?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have quite a few problems, probably misunderstandings, about the new keyword with properties in derived classes. Most of the documentation deals with new on derived methods. For instance: [DataContract] [KnownType(typeof(PostCategory))] public class BaseCategory : IEquatable { \/\/&#8230; \/\/\/ \/\/\/ Enables access to child categories (immediate ones). Filled in by data access provider. Not serializable. [&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-3805","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3805","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=3805"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3805\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3805"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3805"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3805"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}