<f{"id":163,"date":"2013-02-08T00:44:01","date_gmt":"2013-02-07T23:44:01","guid":{"rendered":"http:\/\/www.bernhard-krenz.de\/?p=163"},"modified":"2013-02-08T00:46:11","modified_gmt":"2013-02-07T23:46:11","slug":"instanz-aus-klassennamen-erzeugen","status":"publish","type":"post","link":"https:\/\/www.bernhard-krenz.de\/?p=163","title":{"rendered":"Instanz aus Klassennamen erzeugen"},"content":{"rendered":"<p>Nehmen wir folgenden Fall an: Der Name einer Klasse liegt als string vor und es gilt eine Instanz der Klasse zu erzeugen.<\/p>\n<p>Das kann zum Beispiel so funktionieren:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ you got the name of the class from somewhere\r\nstring className = &quot;Message&quot;;\r\n\/\/ create the type of the class using the destinated namespace\r\nvar type = Type.GetType(&quot;ValueObjects.&quot; + className, false); \r\n\/\/ create the instance now\r\nvar message = Activator.CreateInstance(type) as Message;\r\n<\/pre>\n<p>In ASP.Net funktioniert das allerdings nicht. Hier ist ein Workarund \u00fcber den BuildManager (using System.Web.Compilation) notwendig:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ you got the name of the class from somewhere\r\nstring className = &quot;Message&quot;;\r\n\/\/ create the type of the class using the destinated namespace\r\nvar type = BuildManager.GetType(&quot;ValueObjects.&quot; + className, false);\r\n\/\/ create the instance now\r\nvar message = Activator.CreateInstance(type) as Message;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Nehmen wir folgenden Fall an: Der Name einer Klasse liegt als string vor und es gilt eine Instanz der Klasse<a href=\"https:\/\/www.bernhard-krenz.de\/?p=163\" class=\"searchmore\">Weiterlesen&hellip;<\/a><\/p>\n<div class=\"clr\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[9,7],"_links":{"self":[{"href":"https:\/\/www.bernhard-krenz.de\/index.php?rest_route=\/wp\/v2\/posts\/163"}],"collection":[{"href":"https:\/\/www.bernhard-krenz.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bernhard-krenz.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bernhard-krenz.de\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bernhard-krenz.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=163"}],"version-history":[{"count":4,"href":"https:\/\/www.bernhard-krenz.de\/index.php?rest_route=\/wp\/v2\/posts\/163\/revisions"}],"predecessor-version":[{"id":166,"href":"https:\/\/www.bernhard-krenz.de\/index.php?rest_route=\/wp\/v2\/posts\/163\/revisions\/166"}],"wp:attachment":[{"href":"https:\/\/www.bernhard-krenz.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bernhard-krenz.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bernhard-krenz.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}