{"id":3996,"date":"2014-03-30T07:04:03","date_gmt":"2014-03-30T07:04:03","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/dynamic-cast-using-type-of-object-c-collection-of-common-programming-errors\/"},"modified":"2014-03-30T07:04:03","modified_gmt":"2014-03-30T07:04:03","slug":"dynamic-cast-using-type-of-object-c-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/dynamic-cast-using-type-of-object-c-collection-of-common-programming-errors\/","title":{"rendered":"Dynamic cast using Type of object C#-Collection of common programming errors"},"content":{"rendered":"<p>I have one abstract class named A, and other classes (B, C, D, E, &#8230;) that implements A<\/p>\n<p>I also have a list of A objects.<br \/>\nI&#8217;d like to be able to cast dynamicly each of the object in that list to their &#8220;base&#8221; type (ie B, C, D, &#8230;) to be able to call their constructor in an other method.<\/p>\n<p>Here is what I have done for now :<\/p>\n<pre><code>abstract class A { }\nclass B : A { }\nclass C : A { }\nclass D : A { }\nclass E : A { }\n\/\/ ... \n\nclass Program\n{\n    static void Main(string[] args)\n    {\n        List list = new List { new B(), new C(), new D(), new E() };\n        \/\/ ...\n\n        foreach (A item in list)\n        {\n            A obj  = foo(item);\n        }\n    }\n\n    public static A foo(A obj)\n    {\n        if (obj.GetType() == typeof(B))\n        {\n            return bar((B)obj);\n        }\n        else if (obj.GetType() == typeof(C))\n        {\n            return bar((C)obj);\n        }\n        \/\/ ... same for D, E, ...\n        return null;\n    }\n\n    public static T bar(T obj) where T : class, new()\n    {\n        \/\/ To use the constructor, I can't have here an abstract class.\n        T newObj = new T();\n        return newObj;\n    }\n<\/code><\/pre>\n<p>It works, but I&#8217;d like to find an other way but to test for each class that implements A if their type equals the type of my object, and cast it afterwards.<\/p>\n<p>I have nearly 15 classes like B, C, D, &#8230; and I might have more. In order to have something simple, clear and maintainable, I&#8217;d like to avoid this methods, and the 15+ &#8220;if(&#8230;) else(&#8230;)&#8221;.<\/p>\n<p>Do you see a way to do so ?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have one abstract class named A, and other classes (B, C, D, E, &#8230;) that implements A I also have a list of A objects. I&#8217;d like to be able to cast dynamicly each of the object in that list to their &#8220;base&#8221; type (ie B, C, D, &#8230;) to be able to call [&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-3996","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3996","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=3996"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3996\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}