{"id":46,"date":"2009-06-04T17:16:35","date_gmt":"2009-06-04T16:16:35","guid":{"rendered":"http:\/\/www.manuel-then.de\/?p=46"},"modified":"2009-11-12T16:03:27","modified_gmt":"2009-11-12T15:03:27","slug":"typebuilder-is-a-type","status":"publish","type":"post","link":"https:\/\/www.manuel-then.de\/?p=46","title":{"rendered":"TypeBuilder is a Type!"},"content":{"rendered":"<p>Today, although it would have been quite obvious by simply reading the MSDN (yes, RTFM fits in this situation), I spent some minutes searching for a way to dynamically create two mutually referencing classes via <i>TypeBuilder<\/i>. What I was looking for was a way to generate something like:<br \/>\n<code><br \/>\nclass A { public B b; }<br \/>\nclass B { public A a; }<br \/>\n<\/code><br \/>\nMy first attempt was to create <i>A<\/i> without any fields, use the type to generate <i>B<\/i> and modify the former afterwards.  As one cannot change a type once <i>TypeBuilder.CreateType()<\/i> has been called, that attempt failed.<br \/>\nAfter some searching I found out that <i>TypeBuilder<\/i> is a subclass of <i>Type<\/i>. Knowing this, the initial problem is easily solvable through:<br \/>\n <code><br \/>\nTypeBuilder aBuilder = moduleBuilder.DefineType(\u2026);<br \/>\nTypeBuilder bBuilder = moduleBuilder.DefineType(\u2026);<br \/>\naBuilder.DefineField(\"b\", bBuilder, FieldAttributes.Public);<br \/>\nbBuilder.DefineField(\"a\", aBuilder, FieldAttributes.Public);<br \/>\naBuilder.CreateType();<br \/>\nbBuilder.CreateType();<br \/>\n<\/code><br \/>\nOnce again the simplest solution was the best.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, although it would have been quite obvious by simply reading the MSDN (yes, RTFM fits in this situation), I spent some minutes searching for a way to dynamically create two mutually referencing classes via TypeBuilder. What I was looking for was a way to generate something like: class A { public B b; } [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8,12,7],"tags":[],"_links":{"self":[{"href":"https:\/\/www.manuel-then.de\/index.php?rest_route=\/wp\/v2\/posts\/46"}],"collection":[{"href":"https:\/\/www.manuel-then.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.manuel-then.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.manuel-then.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.manuel-then.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=46"}],"version-history":[{"count":3,"href":"https:\/\/www.manuel-then.de\/index.php?rest_route=\/wp\/v2\/posts\/46\/revisions"}],"predecessor-version":[{"id":99,"href":"https:\/\/www.manuel-then.de\/index.php?rest_route=\/wp\/v2\/posts\/46\/revisions\/99"}],"wp:attachment":[{"href":"https:\/\/www.manuel-then.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=46"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.manuel-then.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=46"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.manuel-then.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=46"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}