{"id":477,"date":"2009-07-17T15:02:03","date_gmt":"2009-07-17T20:02:03","guid":{"rendered":"http:\/\/www.thegatesofdawn.ca\/wordpress\/?p=477"},"modified":"2009-07-17T15:02:26","modified_gmt":"2009-07-17T20:02:26","slug":"delay-loading-assemblies-in-net","status":"publish","type":"post","link":"https:\/\/www.thegatesofdawn.ca\/wordpress\/posts\/2009\/07\/17\/delay-loading-assemblies-in-net\/","title":{"rendered":"Delay-loading assemblies in .NET"},"content":{"rendered":"<p>I was trying to find a .NET equivalent to the &#8220;\/delayload&#8221; option that the linker has for unmanaged code.  For the uninitiated, &#8220;\/delayload:someDLL.dll&#8221; changes the resulting .EXE\/.DLL so that someDLL.dll will not be loaded until the first time a function in it is called.  It&#8217;s a handy option you can use so that you do not need to install someDLL.dll on computers where you know it&#8217;s not going to be needed.<br \/>\n<!--more--><br \/>\nI wanted to find a way to the same thing with .NET assemblies.  I have an Assembly1.DLL which will sometimes call into an Assembly2.DLL.  But not always.  And on computers where Assembly2.DLL will never be called, I don&#8217;t want to install it on the system at all.<\/p>\n<p>I couldn&#8217;t find any equivalent to &#8220;\/delayload:Assembly2.DLL&#8221;.  I figured that it would be possible to achieve the desired effect using a proxy class with <code>System.Assembly.Load<\/code> and <code>System.Type.InvokeMember<\/code>.  I couldn&#8217;t find any existing code on the Internet that did this, so I set about writing one.  I called it <code>DelayLoadProxy<\/code>.  It was using &#8220;method intercept&#8221; techniques and Reflection and attributes, and was going to be all cool and nifty.  Development was going along really well, for about a day and half.<\/p>\n<p>Then I thought, maybe I should go talk to our resident .NET expert, see if he knows of any existing code that does this.  But the answer was worse than I could have imagined.  In fact, &#8220;\/delayload&#8221; is the default behaviour of .NET!<\/p>\n<p>That&#8217;s right&#8230; you don&#8217;t need &#8220;\/delayload&#8221; at all.  By default, an assembly is not loaded at all until it&#8217;s time to run code from it.  If you never run code in it, then the assembly need not even be present on the target system; nobody will ever look for it.<\/p>\n<p>So all that work was for nothing.<\/p>\n<p>I guess my approach still has the trivial advantage that you don&#8217;t even need the second assembly to be present even when compiling the first assembly.  Whatever&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was trying to find a .NET equivalent to the &#8220;\/delayload&#8221; option that the linker has for unmanaged code. For the uninitiated, &#8220;\/delayload:someDLL.dll&#8221; changes the resulting .EXE\/.DLL so that someDLL.dll will not be loaded until the first time a function in it is called. It&#8217;s a handy option you can use so that you do [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[34,39,38,37,40,36],"class_list":["post-477","post","type-post","status-publish","format-standard","hentry","category-tech","tag-net","tag-assemblies","tag-assembly","tag-delay-load","tag-delayload","tag-demand-load"],"_links":{"self":[{"href":"https:\/\/www.thegatesofdawn.ca\/wordpress\/wp-json\/wp\/v2\/posts\/477"}],"collection":[{"href":"https:\/\/www.thegatesofdawn.ca\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.thegatesofdawn.ca\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.thegatesofdawn.ca\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.thegatesofdawn.ca\/wordpress\/wp-json\/wp\/v2\/comments?post=477"}],"version-history":[{"count":3,"href":"https:\/\/www.thegatesofdawn.ca\/wordpress\/wp-json\/wp\/v2\/posts\/477\/revisions"}],"predecessor-version":[{"id":634,"href":"https:\/\/www.thegatesofdawn.ca\/wordpress\/wp-json\/wp\/v2\/posts\/477\/revisions\/634"}],"wp:attachment":[{"href":"https:\/\/www.thegatesofdawn.ca\/wordpress\/wp-json\/wp\/v2\/media?parent=477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thegatesofdawn.ca\/wordpress\/wp-json\/wp\/v2\/categories?post=477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thegatesofdawn.ca\/wordpress\/wp-json\/wp\/v2\/tags?post=477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}