
{"id":4603,"date":"2019-12-12T13:24:20","date_gmt":"2019-12-12T18:24:20","guid":{"rendered":"https:\/\/ikriv.com\/blog\/?p=4603"},"modified":"2019-12-12T13:24:20","modified_gmt":"2019-12-12T18:24:20","slug":"net-type-forwarding","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=4603","title":{"rendered":".NET type forwarding"},"content":{"rendered":"<p>This feature has apparently existed for years, but I found out only now. CLR has an equivalent of HTTP redirect for types, it is called You can move a class from one assembly to another, it is called <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.runtime.compilerservices.typeforwardedtoattribute?view=netframework-4.8\">TypeForwardedTo attribute<\/a>.<\/p>\n<p>If type T has been moved from assembly A to assembly B, new version of assembly A may reference assembly B, and include <code>[assembly:TypeForwardedTo(typeof(Some.Namespace.T)]<\/code> attribute. Full type name, including namespace, must be the same in A and B. Whenever someone requests <code>Some.Namepsace.T, A<\/code>, CLR silently gives them <code>Some.Namespace.T, B<\/code> instead. It is OK for A and B to be strongly named and signed by different keys.<\/p>\n<p>An example is on GitHub: <a href=\"https:\/\/github.com\/ikriv-samples\/TypeForwardingTest\">https:\/\/github.com\/ikriv-samples\/TypeForwardingTest<\/a>.<\/p>\n<p>Apparently, this is extensively used in .NetStandard as explained in <a href=\"https:\/\/youtu.be\/vg6nR7hS2lI\">this video<\/a>. At compile time class libraries targeting .NetStandard reference <code>dotnetstandard.dll<\/code> that contains empty implementations of classes, and at runtime they are given another version of <codE>dotnetstandard.dll<\/code> that type-forwards those impelmentations to the real ones on the current platform, e.g. <code>System.String, dotnetstandard<\/code> becomes <code>System.String, mscorlib<\/code> on regular .NET and <code>System.String, System.Runtime<\/code> on .NET Core.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This feature has apparently existed for years, but I found out only now. CLR has an equivalent of HTTP redirect for types, it is called You can move a class <a href=\"https:\/\/ikriv.com\/blog\/?p=4603\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[3,8,4],"tags":[],"class_list":["entry","author-ikriv","post-4603","post","type-post","status-publish","format-standard","category-dotnet","category-cs","category-hack"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4603","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4603"}],"version-history":[{"count":1,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4603\/revisions"}],"predecessor-version":[{"id":4604,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4603\/revisions\/4604"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}