{"id":494,"date":"2010-03-28T19:49:39","date_gmt":"2010-03-28T23:49:39","guid":{"rendered":"http:\/\/www.insidethe.com\/blog\/?p=494"},"modified":"2010-03-28T19:49:39","modified_gmt":"2010-03-28T23:49:39","slug":"loading-specific-versions-of-net-assemblies-from-the-gac-based-on-the-assembly-information-in-a-net-dll-example-code","status":"publish","type":"post","link":"https:\/\/insidethe.com\/blog\/2010\/03\/loading-specific-versions-of-net-assemblies-from-the-gac-based-on-the-assembly-information-in-a-net-dll-example-code\/","title":{"rendered":"Loading Specific Versions of .NET Assemblies From the GAC Based on the Assembly Information in a .NET DLL &#8211; Visual Basic Example Code"},"content":{"rendered":"<p>By default the latest version of an assembly is loaded from the GAC (Global Assembly Cache) when the assembly is instantiated. While working on a side project the need arose to load older versions of the assembly without hard coding the assembly versions. The answer, load the existing assembly library from the assembly DLL and interrogate it to find the assembly information. Then ask the GAC to provide a handle to the specific object. The result allows multiple versions of the same assembly to be loaded loaded at the same time.<\/p>\n<p>The Visual Basic .NET example code below loads SampleAppV1\\DotNetLibrary.DLL and AnotherSampleAppV2\\DotNetLibrary.DLL which are different versions of the same assembly.<\/p>\n<pre><span style=\"color: #008000;\">'Load assembly using reflection from the file specified so we can get information about it<\/span>\r\n<span style=\"color: #0000a0;\">Dim<\/span> DLLAssemblyInfo <span style=\"color: #0000a0;\">As<\/span> System.Reflection.Assembly\r\nDLLAssemblyInfo = System.Reflection.Assembly.LoadFrom<span style=\"color: #c00000;\">(<\/span><span style=\"color: #008080;\">\"\\\\\\\\appserver.company.int\\\\SampleAppV1\\\\DotNetLibrary.DLL\"<\/span><span style=\"color: #c00000;\">)<\/span>\r\n\r\n<span style=\"color: #008000;\">'Now load the assembly from the GAC using the information in the file. <\/span>\r\n<span style=\"color: #0000a0;\">Dim<\/span> GACDLL <span style=\"color: #0000a0;\">As<\/span> System.Reflection.Assembly\r\nGACDLL = System.Reflection.Assembly.Load<span style=\"color: #c00000;\">(<\/span>AssemblyInfo.FullName, DLLAssemblyInfo.Evidence<span style=\"color: #c00000;\">)<\/span><\/pre>\n<pre><span style=\"color: #008000;\">'Load the second version of the same assembly from the newer application\r\n<span style=\"color: #000000;\"><span style=\"color: #0000a0;\">Dim<\/span> DLLAssemblyInfoV2 <span style=\"color: #0000a0;\">As<\/span> System.Reflection.Assembly\r\nDLLAssemblyInfoV2 = System.Reflection.Assembly.LoadFrom<span style=\"color: #c00000;\">(<\/span><span style=\"color: #008080;\">\"\\\\\\\\appserver.company.int\\\\AnotherSampleAppV2\\\\DotNetLibrary.DLL\"<\/span><span style=\"color: #c00000;\">)<\/span><\/span><\/span><\/pre>\n<pre><span style=\"color: #008000;\">'Again, use the DLL information from the assembly file to load the assembly from the GAC<\/span>\r\n<span style=\"color: #0000a0;\">Dim<\/span> GACDLLv2 <span style=\"color: #0000a0;\">As<\/span> System.Reflection.Assembly\r\nGACDLLv2 = System.Reflection.Assembly.Load<span style=\"color: #c00000;\">(<\/span>AssemblyInfoV2.FullName, DLLAssemblyInfoV2.Evidence<span style=\"color: #c00000;\">)<\/span><\/pre>\n<pre><span style=\"color: #008000;\">'At this point in the code we have references to both assemblies loaded from the GAC. The next step is to instantiate an object from each of the references.<\/span><\/pre>\n<pre><span style=\"color: #008000;\">'Get an MyAssembly.Library from the old version and run the version method<\/span>\r\n<span style=\"color: #0000a0;\">Dim<\/span> GACDLLType <span style=\"color: #0000a0;\">As Type<\/span> = GACDLL.GetType<span style=\"color: #c00000;\">(<\/span><span style=\"color: #008080;\">\"MyAssembly.Library\"<\/span><span style=\"color: #c00000;\">)<\/span>\r\n<span style=\"color: #0000a0;\">Dim<\/span> MyLibV1 <span style=\"color: #0000a0;\">As Object<\/span> = Activator.CreateInstance<span style=\"color: #c00000;\">(<\/span>GACDLLType<span style=\"color: #c00000;\">)<\/span>\r\n<span style=\"color: #0000a0;\">Dim<\/span> v1 <span style=\"color: #0000a0;\">As String\r\n<span style=\"color: #000000;\">v1 = MyLibV1.Version<\/span><\/span><\/pre>\n<pre><span style=\"color: #008000;\">'Finally, do the same and get the new MyAssembly.Library<\/span>\r\n<span style=\"color: #0000a0;\">Dim<\/span> GACDLLTypeV2 <span style=\"color: #0000a0;\">As Type<\/span> = GACDLLV2.GetType<span style=\"color: #c00000;\">(<\/span><span style=\"color: #008080;\">\"MyAssembly.Library\"<\/span><span style=\"color: #c00000;\">)<\/span>\r\n<span style=\"color: #0000a0;\">Dim<\/span> MyLibV2 <span style=\"color: #0000a0;\">As Object<\/span> = Activator.CreateInstance<span style=\"color: #c00000;\">(<\/span>GACDLLTypeV2<span style=\"color: #c00000;\">)<\/span>\r\n<span style=\"color: #0000a0;\">Dim<\/span> v2 <span style=\"color: #0000a0;\">As String\r\n<span style=\"color: #000000;\">v2 = MyLibV2.Version<\/span><\/span><\/pre>\n<p><span style=\"color: #0000a0;\"><span style=\"color: #000000;\"> <\/span><\/span><\/p>\n<p><span style=\"color: #0000a0;\"><span style=\"color: #000000;\">The variables v1 and v2 were both retrieved from the MyAssembly.Library.Version() method but the variables will not contain the same value because different versions of the assembly were loaded from the GAC during the System.Reflection.Assembly.Load() call. <\/span><\/span><\/p>\n<p>Why not just load the assembly from each of the DLL files instead of the GAC? Security restrictions prevented untrusted assemblies from being loaded over the network. A possible work around would be to trust the network location or sign the assemblies and trust the signer but that remains for another posting.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>By default the latest version of an assembly is loaded from the GAC (Global Assembly Cache) when the assembly is instantiated. While working on a side project the need arose to load older versions of the assembly without hard coding the assembly versions. The answer, load the existing assembly library from the assembly DLL and [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[11,17],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Loading Specific Versions of .NET Assemblies From the GAC Based on the Assembly Information in a .NET DLL - Visual Basic Example Code - Insidethe.com<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/insidethe.com\/blog\/2010\/03\/loading-specific-versions-of-net-assemblies-from-the-gac-based-on-the-assembly-information-in-a-net-dll-example-code\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Loading Specific Versions of .NET Assemblies From the GAC Based on the Assembly Information in a .NET DLL - Visual Basic Example Code - Insidethe.com\" \/>\n<meta property=\"og:description\" content=\"By default the latest version of an assembly is loaded from the GAC (Global Assembly Cache) when the assembly is instantiated. While working on a side project the need arose to load older versions of the assembly without hard coding the assembly versions. The answer, load the existing assembly library from the assembly DLL and [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/insidethe.com\/blog\/2010\/03\/loading-specific-versions-of-net-assemblies-from-the-gac-based-on-the-assembly-information-in-a-net-dll-example-code\/\" \/>\n<meta property=\"og:site_name\" content=\"Insidethe.com\" \/>\n<meta property=\"article:published_time\" content=\"2010-03-28T23:49:39+00:00\" \/>\n<meta name=\"author\" content=\"Jared\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jared\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/insidethe.com\/blog\/2010\/03\/loading-specific-versions-of-net-assemblies-from-the-gac-based-on-the-assembly-information-in-a-net-dll-example-code\/\",\"url\":\"https:\/\/insidethe.com\/blog\/2010\/03\/loading-specific-versions-of-net-assemblies-from-the-gac-based-on-the-assembly-information-in-a-net-dll-example-code\/\",\"name\":\"Loading Specific Versions of .NET Assemblies From the GAC Based on the Assembly Information in a .NET DLL - Visual Basic Example Code - Insidethe.com\",\"isPartOf\":{\"@id\":\"https:\/\/insidethe.com\/blog\/#website\"},\"datePublished\":\"2010-03-28T23:49:39+00:00\",\"dateModified\":\"2010-03-28T23:49:39+00:00\",\"author\":{\"@id\":\"https:\/\/insidethe.com\/blog\/#\/schema\/person\/046e8441af869a735c4e31ac51541a99\"},\"breadcrumb\":{\"@id\":\"https:\/\/insidethe.com\/blog\/2010\/03\/loading-specific-versions-of-net-assemblies-from-the-gac-based-on-the-assembly-information-in-a-net-dll-example-code\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/insidethe.com\/blog\/2010\/03\/loading-specific-versions-of-net-assemblies-from-the-gac-based-on-the-assembly-information-in-a-net-dll-example-code\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/insidethe.com\/blog\/2010\/03\/loading-specific-versions-of-net-assemblies-from-the-gac-based-on-the-assembly-information-in-a-net-dll-example-code\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/insidethe.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Loading Specific Versions of .NET Assemblies From the GAC Based on the Assembly Information in a .NET DLL &#8211; Visual Basic Example Code\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/insidethe.com\/blog\/#website\",\"url\":\"https:\/\/insidethe.com\/blog\/\",\"name\":\"Insidethe.com\",\"description\":\"It is my space, but better than myspace\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/insidethe.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/insidethe.com\/blog\/#\/schema\/person\/046e8441af869a735c4e31ac51541a99\",\"name\":\"Jared\",\"sameAs\":[\"https:\/\/www.insidethe.com\/blog\"],\"url\":\"https:\/\/insidethe.com\/blog\/author\/jared\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Loading Specific Versions of .NET Assemblies From the GAC Based on the Assembly Information in a .NET DLL - Visual Basic Example Code - Insidethe.com","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/insidethe.com\/blog\/2010\/03\/loading-specific-versions-of-net-assemblies-from-the-gac-based-on-the-assembly-information-in-a-net-dll-example-code\/","og_locale":"en_US","og_type":"article","og_title":"Loading Specific Versions of .NET Assemblies From the GAC Based on the Assembly Information in a .NET DLL - Visual Basic Example Code - Insidethe.com","og_description":"By default the latest version of an assembly is loaded from the GAC (Global Assembly Cache) when the assembly is instantiated. While working on a side project the need arose to load older versions of the assembly without hard coding the assembly versions. The answer, load the existing assembly library from the assembly DLL and [&hellip;]","og_url":"https:\/\/insidethe.com\/blog\/2010\/03\/loading-specific-versions-of-net-assemblies-from-the-gac-based-on-the-assembly-information-in-a-net-dll-example-code\/","og_site_name":"Insidethe.com","article_published_time":"2010-03-28T23:49:39+00:00","author":"Jared","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Jared","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/insidethe.com\/blog\/2010\/03\/loading-specific-versions-of-net-assemblies-from-the-gac-based-on-the-assembly-information-in-a-net-dll-example-code\/","url":"https:\/\/insidethe.com\/blog\/2010\/03\/loading-specific-versions-of-net-assemblies-from-the-gac-based-on-the-assembly-information-in-a-net-dll-example-code\/","name":"Loading Specific Versions of .NET Assemblies From the GAC Based on the Assembly Information in a .NET DLL - Visual Basic Example Code - Insidethe.com","isPartOf":{"@id":"https:\/\/insidethe.com\/blog\/#website"},"datePublished":"2010-03-28T23:49:39+00:00","dateModified":"2010-03-28T23:49:39+00:00","author":{"@id":"https:\/\/insidethe.com\/blog\/#\/schema\/person\/046e8441af869a735c4e31ac51541a99"},"breadcrumb":{"@id":"https:\/\/insidethe.com\/blog\/2010\/03\/loading-specific-versions-of-net-assemblies-from-the-gac-based-on-the-assembly-information-in-a-net-dll-example-code\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/insidethe.com\/blog\/2010\/03\/loading-specific-versions-of-net-assemblies-from-the-gac-based-on-the-assembly-information-in-a-net-dll-example-code\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/insidethe.com\/blog\/2010\/03\/loading-specific-versions-of-net-assemblies-from-the-gac-based-on-the-assembly-information-in-a-net-dll-example-code\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/insidethe.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Loading Specific Versions of .NET Assemblies From the GAC Based on the Assembly Information in a .NET DLL &#8211; Visual Basic Example Code"}]},{"@type":"WebSite","@id":"https:\/\/insidethe.com\/blog\/#website","url":"https:\/\/insidethe.com\/blog\/","name":"Insidethe.com","description":"It is my space, but better than myspace","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/insidethe.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/insidethe.com\/blog\/#\/schema\/person\/046e8441af869a735c4e31ac51541a99","name":"Jared","sameAs":["https:\/\/www.insidethe.com\/blog"],"url":"https:\/\/insidethe.com\/blog\/author\/jared\/"}]}},"_links":{"self":[{"href":"https:\/\/insidethe.com\/blog\/wp-json\/wp\/v2\/posts\/494"}],"collection":[{"href":"https:\/\/insidethe.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/insidethe.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/insidethe.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/insidethe.com\/blog\/wp-json\/wp\/v2\/comments?post=494"}],"version-history":[{"count":7,"href":"https:\/\/insidethe.com\/blog\/wp-json\/wp\/v2\/posts\/494\/revisions"}],"predecessor-version":[{"id":502,"href":"https:\/\/insidethe.com\/blog\/wp-json\/wp\/v2\/posts\/494\/revisions\/502"}],"wp:attachment":[{"href":"https:\/\/insidethe.com\/blog\/wp-json\/wp\/v2\/media?parent=494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/insidethe.com\/blog\/wp-json\/wp\/v2\/categories?post=494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/insidethe.com\/blog\/wp-json\/wp\/v2\/tags?post=494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}