Loading Specific Versions of .NET Assemblies From the GAC Based on the Assembly Information in a .NET DLL – Visual Basic Example Code
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.
The Visual Basic .NET example code below loads SampleAppV1\DotNetLibrary.DLL and AnotherSampleAppV2\DotNetLibrary.DLL which are different versions of the same assembly.
'Load assembly using reflection from the file specified so we can get information about it Dim DLLAssemblyInfo As System.Reflection.Assembly DLLAssemblyInfo = System.Reflection.Assembly.LoadFrom("\\\\appserver.company.int\\SampleAppV1\\DotNetLibrary.DLL") 'Now load the assembly from the GAC using the information in the file. Dim GACDLL As System.Reflection.Assembly GACDLL = System.Reflection.Assembly.Load(AssemblyInfo.FullName, DLLAssemblyInfo.Evidence)
'Load the second version of the same assembly from the newer application
Dim DLLAssemblyInfoV2 As System.Reflection.Assembly
DLLAssemblyInfoV2 = System.Reflection.Assembly.LoadFrom("\\\\appserver.company.int\\AnotherSampleAppV2\\DotNetLibrary.DLL")
'Again, use the DLL information from the assembly file to load the assembly from the GAC Dim GACDLLv2 As System.Reflection.Assembly GACDLLv2 = System.Reflection.Assembly.Load(AssemblyInfoV2.FullName, DLLAssemblyInfoV2.Evidence)
'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.
'Get an MyAssembly.Library from the old version and run the version method Dim GACDLLType As Type = GACDLL.GetType("MyAssembly.Library") Dim MyLibV1 As Object = Activator.CreateInstance(GACDLLType) Dim v1 As String v1 = MyLibV1.Version
'Finally, do the same and get the new MyAssembly.Library Dim GACDLLTypeV2 As Type = GACDLLV2.GetType("MyAssembly.Library") Dim MyLibV2 As Object = Activator.CreateInstance(GACDLLTypeV2) Dim v2 As String v2 = MyLibV2.Version
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.
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.
Story From A Favorite Book: “The Laying On of Hands”
A PRETTY young woman came in to the rabbi.
“Bless me, Rabbi!” she implored.
The rabbi spread his hands over her head and blessed her, but he took care not to touch her head while doing so.
“Why don’t you place your hands on my head?” she asked in surprise. “Blessings from a distance aren’t as fruitful as blessings from near.”
“How do you light the candles on Friday night?” asked the rabbi. “Do you recite the prayer with your hands touching the flame?”
“Of course not-I’d burn my hands!” answered the young woman.
“Believe me, it’s no different with me!” said the rabbi smiling. “Where I to lay my hands on your head, I might also burn them!”An excerpt from A Treasury of Jewish Folklore. Copyright 1948 by Crown Publishers. “The Laying On of Hands”, page 170.
The story immediately begins by describing the woman as simply “a pretty young woman”. The minds eye is left to decide what that may mean. By the end of the story after reading how prudently careful and modest the rabbi has to act towards her the young womans beauty is magnified a hundred fold with no need for further description.
First Magnolia Blooms Of 2010
Several days of March rain showers have coaxed the signs of wonderful color from the earth.
Up until this point what I thought were Dogwood trees are actually Magnolia trees. Behold, the first blooming Magnolia tree of the year.
Costa Rica Beach Panorama
Freehand panorama of a beach in Costa Rica. 20 images stitched in Windows Live Photo Gallery.
Stitching Images – The Panorama Begins
Ever since the 1996 Atlanta Olympics I’ve been enthralled with the freedom of 360 degree and wide aspect panoramic pictures. Apple had recently released QuickTime VR and used it to create 360 degree photos of the Atlanta Olympics. Viewing the photos across my dial-up modem was a chore but the reward of exploring the environment and choosing where to look made the task worth the effort. Instead of being captivate to the photographer’s intended target I was free to look in the other direction for a complete sense of the location. Apple was the first, as I recall, to pioneer this type of technology and make it available to the general public
Unfortunately I didn’t have a Mac and couldn’t generate content on my own. Later years I dabbled with PTgui and some other stitching software that came with a camera. The results were impressive but I wasn’t ready to invest in a high quality camera and the software to generate the images. The Panorama Tools project was interesting but between 2000 and 2002 the tools were too complex to bother using.
Over the past year my interest in panoramic pictures was refreshed by projects like GigaPan and Spherical Panoramas. It was possible for me to tinker with multi-picture stitched panoramic pictures from my point-and-shoot camera because Windows Vista and Windows 7 included a free panorama stitcher in Windows Live Photo Gallery. While on vacation I free handed 20 picture panorama from a beach in Costa Rica with my brother’s digital SLR camera. The results were amazing and I knew it was worth upgrading and buying the proper software.
The “Projects -> Photography” category is an ongoing blog of my adventures with stitched panoramic and 360 degree pictures.