Ranjan Sakalley

Sponsors

The Lounge

Wicked Cool Jobs

News

  • CodeBetter.Com Home



    I'm test-driven!



Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Loading two versions of an assembly that are not in the GAC

Many a time you might face this issue. Especially when you are using a third party component, which uses an older version of an assembly, of which you are using a newer version. Pre 2.0 (and I do not know for sure of 2.0) compiler compliant to the CLS do not support the functionality of a static reference of two differently versioned assemblies with the same name at compile time.

The safest way to do this is Load the assembly object at runtime using Assembly.Load and then calling the appropriate methods. Do not forget to give different display names to these. If the assemblies have strong names and they are Loaded then, they are treated as different assemblies by the CLR. If the assemblies are not strong names, even two identical assemblies loaded from different paths are considered two different assemblies. In both the cases (strongly named or not) even the identical types in the two assemblies are not castable to each other.  
In other words, you have an Assembly X v1.0 with type A and type B. Type B changed with X v2.0 but type A did not. Even so, if you create an object of type A using the reference of Assembly X  v1.0, you will not be able to cast it to the identical Type A of the Assembly X v 2.0. So do not design your application in such a way that you need to cast types in two different assemblies. Or otherwise, include a casting provider.

Code

Lets say we have an assembly AssemblyX. Version 1.0.0.0 has the following classes

public class A
             {
                         public void WriteToConsole()
                         {
                                      Console.WriteLine(" Class A, AssemblyX version 1.0 " );
                         }
             }

public class B
	{
		public void WriteToConsole()
		{
			Console.WriteLine(" Class B, AssemblyX version 1.0 " );
		}
	}

        Version 2.0.0.0 of AssemblyX has the following classes

      
public class A
	{
		public void WriteToConsole()
		{
			Console.WriteLine(" Class A, AssemblyX version 2.0 " );
		}
	}

 

public class B
	{
		public void WriteToConsoleV2()
		{
			Console.WriteLine(" Class B, AssemblyX version 2.0 " );
		}
	}

 AssemblyX v1.0.0.0 is already added as a static reference to our project, lets call it AssemblyLoader (console application), directly or indirectly. Following is the code I have used to load the newer version too, along with the older one, in the same AppDomain,

public class Loader
	{
		static void Main()
		{
			Loader _loader = new Loader();
			_loader.LoadAndRunMethodsV1();
			_loader.LoadAndRunMethodsV2();
			_loader.TryCastA();

			Console.ReadLine();
		}

		public void LoadAndRunMethodsV1()
		{
			AssemblyX.A _a = new A();
			AssemblyX.B _b = new B();

			_a.WriteToConsole();
			_b.WriteToConsole();

		}
		public void LoadAndRunMethodsV2()
		{
			Assembly _assembly = 
Assembly.LoadFrom(@"E:\Blog\Assemblies\v2\AssemblyX.dll"); Type Av2 = _assembly.GetType("AssemblyX.A"); Type Bv2 = _assembly.GetType("AssemblyX.B"); object _a = Activator.CreateInstance(Av2); object _b = Activator.CreateInstance(Bv2); MethodInfo _mA = Av2.GetMethod("WriteToConsole"); MethodInfo _mB = Bv2.GetMethod("WriteToConsoleV2"); _mA.Invoke(_a,null); _mB.Invoke(_b,null); } public void TryCastA() { try { Assembly _assembly =
Assembly.LoadFrom(@"E:\Blog\Assemblies\v2\AssemblyX.dll"); Type Av2 = _assembly.GetType("AssemblyX.A"); object _a = Activator.CreateInstance(Av2); Console.WriteLine(typeof(AssemblyX.A).AssemblyQualifiedName); Console.WriteLine(Av2.AssemblyQualifiedName); (_a as AssemblyX.A).WriteToConsole(); } catch(Exception ex) { Console.WriteLine(ex.Message); } } }

When you run the console application, you get the following output. Notice the object reference exception you would get, because of the invalid cast between the the two classes.

 

You would get the same results if you used strongly named assemblies.


Posted Wed, Apr 13 2005 12:04 AM by rsakalley
Filed under:

[Advertisement]

Comments

NeVaL wrote re: Loading two versions of an assembly that are not in the GAC
on Thu, Dec 28 2006 5:53 AM

http://www.l-code.beibi.info ^^^ http://www.l-eos.beibi.info ^^^ http://www.filmrull-anime-porn.biseksuell.info ^^^ http://www.nefertitis-ektemann-galleri.biseksuell.info ^^^ http://www.filmrull-anime-porn.erotiska.info ^^^ http://www.nefertitis-ektemann-galleri.erotiska.info ^^^ http://www.porrstjarna-ung.fitta69.info ^^^ http://www.jpg-modig-moder.fitta69.info ^^^ http://www.dandy-mor-xxx.fotsex.info ^^^ http://www.klipp-hora-slampig.fotsex.info ^^^ http://www.uskalias-lesbo-striptease.isomuna.info ^^^ http://www.otos-black-bbw.isomuna.info ^^^ http://www.natten-kutjes.laukeaminen.info ^^^ http://www.sympaattinen-teini-suihinotto.laukeaminen.info ^^^ http://www.grisete-anal-videoer.rype.info ^^^ http://www.film-lesbian-voyeur.rype.info ^^^ http://www.porno-knulle-bilde.sadsprut.info ^^^ http://www.porn-videos-bilder.sadsprut.info ^^^ http://www.kuuma-banaani-otos.tytsy.info ^^^ http://www.sexjenter-otos.tytsy.info ^^^ http://www.anal-stories-pics.18analsex.com ^^^ http://www.ass-master-movies.18analsex.com ^^^ http://www.filmati-super-hentay.pazzesesso.com ^^^ http://www.alice-vip-paparazzo.pazzesesso.com ^^^ http://www.download-bambine-bocchinare.figanere.com ^^^ http://www.ragazzine-vogliose-xxx.figanere.com ^^^ http://www.ampland-xxx-fotografia.inculatexxx.com ^^^ http://www.fake-celeb-immagini.inculatexxx.com ^^^ http://www.bellezza-cosmesi.prostitutaculo.com ^^^ http://www.gif-gonna-minigonna.prostitutaculo.com ^^^ http://www.galleria-famosa-sex.lesbicastrip.com ^^^ http://www.sexanimal-it-sesso.lesbicastrip.com ^^^ http://www.pics-chupa-pijas.007sexogratis.com ^^^ http://www.clips-cordoba-lola.007sexogratis.com ^^^ http://www.video-sexi.3sexogratis.com ^^^ http://www.super-culo-movies.3sexogratis.com ^^^ http://www.leche-calientecom-mpg.analsexogratis.com ^^^ http://www.petardas-cl.analsexogratis.com ^^^ http://www.flor-bellas-mpg.cam-sexo-gratis.com ^^^ http://www.clips-foro-dz.cam-sexo-gratis.com ^^^

www wrote re: Loading two versions of an assembly that are not in the GAC
on Thu, Jan 18 2007 10:30 AM
dahman haridi wrote re: Loading two versions of an assembly that are not in the GAC
on Thu, Jan 18 2007 3:27 PM

slt tue lmonde

dahman haridi wrote re: Loading two versions of an assembly that are not in the GAC
on Thu, Jan 18 2007 3:29 PM

slt tue lmonde

dahman haridi wrote re: Loading two versions of an assembly that are not in the GAC
on Thu, Jan 18 2007 3:29 PM

slt tue lmonde

vieille suce wrote re: Loading two versions of an assembly that are not in the GAC
on Tue, Jan 23 2007 3:18 AM

http://www.vieille-suce.biz - http://chearley-nue.vieille-suce.biz - http://cheilsy-nue.vieille-suce.biz - http://cheline-nue.vieille-suce.biz - http://chella-nue.vieille-suce.biz - http://chellecie-nue.vieille-suce.biz - http://chelsa-nue.vieille-suce.biz - http://chelsa-ann-nue.vieille-suce.biz - http://chelsea-nue.vieille-suce.biz - http://chelsea-alexa-nue.vieille-suce.biz - http://chelsea-dana-nue.vieille-suce.biz - http://chelsea-mathilde-nue.vieille-suce.biz - http://chelsea-tamar-nue.vieille-suce.biz - http://chelsea-ann-nue.vieille-suce.biz - http://chelsea-cyndel-nue.vieille-suce.biz - http://chelsea-ebony-nue.vieille-suce.biz - http://chelsee-nue.vieille-suce.biz - http://chelsey-nue.vieille-suce.biz - http://chelsey-angel-nue.vieille-suce.biz - http://chelsey-todd-nue.vieille-suce.biz - http://chelsie-ann-nue.vieille-suce.biz - http://chelssy-nue.vieille-suce.biz - http://chelsy-nue.vieille-suce.biz - http://chelthsey-nue.vieille-suce.biz - http://chenille-nue.vieille-suce.biz - http://chenille-carlie-nue.vieille-suce.biz - http://cherasela-nue.vieille-suce.biz - http://cherley-nue.vieille-suce.biz - http://cherlie-nue.vieille-suce.biz - http://cherokia-carlene-nue.vieille-suce.biz - http://cheryl-patricia-nue.vieille-suce.biz - http://cheryline-nue.vieille-suce.biz - http://cheuk-ying-nue.vieille-suce.biz - http://cheyanne-nue.vieille-suce.biz - http://cheyenne-alexus-nue.vieille-suce.biz - http://chia-ling-veronica-nue.vieille-suce.biz - http://chiana-nue.vieille-suce.biz - http://chiara-nue.vieille-suce.biz - http://chiara-natalie-nue.vieille-suce.biz - http://chimene-marina-nue.vieille-suce.biz - http://chiraz-nue.vieille-suce.biz - http://chitron-ireeka-nue.vieille-suce.biz - http://chlea-nue.vieille-suce.biz - http://chloe-nue.vieille-suce.biz - http://chloe-ann-isabelle-nue.vieille-suce.biz - http://chloe-anne-nue.vieille-suce.biz - http://chloe-da-woom-nue.vieille-suce.biz - http://chloe-liliane-nue.vieille-suce.biz - http://chloe-maeva-nue.vieille-suce.biz - http://chloe-n-nue.vieille-suce.biz - http://chloe-sarah-nue.vieille-suce.biz - http://chloee-nue.vieille-suce.biz - http://chloe-eve-nue.vieille-suce.biz - http://chloe-michelle-nue.vieille-suce.biz - http://chole-nue.vieille-suce.biz - http://chontelle-makayla-nue.vieille-suce.biz - http://chonthita-nue.vieille-suce.biz - http://chrisberlie-nue.vieille-suce.biz - http://chrisna-nue.vieille-suce.biz - http://chrissa-leila-nue.vieille-suce.biz - http://christa-nue.vieille-suce.biz - http://christel-nue.vieille-suce.biz - http://christella-nue.vieille-suce.biz - http://christelle-nue.vieille-suce.biz - http://christ-elle-nue.vieille-suce.biz - http://christiane-nue.vieille-suce.biz - http://christiane-rose-nue.vieille-suce.biz - http://christianne-nue.vieille-suce.biz - http://christina-nue.vieille-suce.biz - http://christina-aaliyah-nue.vieille-suce.biz - http://christina-ji-woo-nue.vieille-suce.biz - http://christina-joanne-nue.vieille-suce.biz - http://christina-franceska-nue.vieille-suce.biz - http://christine-nue.vieille-suce.biz - http://christine-michaelle-nue.vieille-suce.biz - http://christopher-nue.vieille-suce.biz - http://chrstine-nue.vieille-suce.biz - http://chrystal-nue.vieille-suce.biz - http://chrystalleni-nue.vieille-suce.biz - http://chrystel-nue.vieille-suce.biz - http://chrystell-nue.vieille-suce.biz - http://chujie-nue.vieille-suce.biz - http://chyna-nue.vieille-suce.biz - http://ciara-nue.vieille-suce.biz - http://cidnee-nue.vieille-suce.biz - http://ciera-nue.vieille-suce.biz - http://cierra-nue.vieille-suce.biz - http://cindy-nue.vieille-suce.biz - http://cindy-bao-xian-nue.vieille-suce.biz - http://cindy-elizabeth-nue.vieille-suce.biz

lublu wrote re: Loading two versions of an assembly that are not in the GAC
on Thu, Jan 25 2007 9:52 AM

http://www.vicek23.lublu.nom.es/agenzia-immobiliare-gruppo/

http://www.ititit.lublu.nom.es/fsex-infermiera.html

http://chiropratica-milano.lublu.nom.es/

http://www.italy-portal.lublu.nom.es/transessuali-frei/

http://www.zaas.lublu.nom.es/hotel-atlantic-pesaro.html

http://www.vigorex.lublu.nom.es/145923924.html

http://www.5veretroie.lublu.nom.es/carino-lieve.html

http://www.5veretroie.lublu.nom.es/mousy-giovane-figa-fotti.html

http://www.virtualepp.lublu.nom.es/renault-usata.html

http://www.5veretroie.lublu.nom.es/intrepido-cowgirl-figa-fotti.html

http://www.uklej.lublu.nom.es/scambiste.html

http://www.5veretroie.lublu.nom.es/assurdo-mature.html

http://www.vicek23.lublu.nom.es/sauna-viso/

http://www.ititit.lublu.nom.es/verona-itinerario.html

http://www.virtualepp.lublu.nom.es/sex-amatoriali-merda.html

http://www.vigorex.lublu.nom.es/145923972.html

http://www.sexy1video.lublu.nom.es/k5bil9jby.html

http://www.sexy1video.lublu.nom.es/e5r1pl347.html

http://www.troie.lublu.nom.es/143848800/

http://www.sexy1video.lublu.nom.es/7kkwprsnm.html

http://www.sexy1video.lublu.nom.es/tbk52ptyna5.html

http://www.dobloo.lublu.nom.es/144961248/

http://www.yoora.lublu.nom.es/caldissimo-casalinga.html

http://www.yoora.lublu.nom.es/eccessivo-agente-di-polizia-dildo.html

http://www.vicek23.lublu.nom.es/neurochirurgia-verona/

http://www.uklej.lublu.nom.es/meeting-rimini.html

http://www.yoora.lublu.nom.es/rabbity-fighetta-orale-fotti.html

http://www.dobloo.lublu.nom.es/144797072/

http://www.vicek23.lublu.nom.es/torino-varsavia/

http://www.20070124.lublu.nom.es/pavimenti-umidita.html

http://www.virtualepp.lublu.nom.es/mara-venier.html

http://www.uklej.lublu.nom.es/lettore-dvd-majestic.html

http://www.beepoo.lublu.nom.es/insensato-cowgirl-sex.html

http://www.5veretroie.lublu.nom.es/milano-nascosta.html

http://www.5veretroie.lublu.nom.es/segretaria-schizzate-di-figa-nella-stanza.html

http://www.yoora.lublu.nom.es/likable-fighetta-figa-fotti.html

http://www.sexy1video.lublu.nom.es/asjdfshw.html

http://www.ititit.lublu.nom.es/trasmissione-uomo-donna.html

http://www.virtualepp.lublu.nom.es/brasex-cameriera-masturbate.html

Devlicio.us