Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I reference two versions of a dll in the same project without putting them in the GAC?

Tags:

c#

.net

Can I reference two versions of a dll in the same project without putting them in the GAC?

Thanks

like image 284
arkina Avatar asked Apr 06 '11 13:04

arkina


People also ask

How do I use two versions of the same DLL in the same project?

config. Under <runtime> , add a <codeBase> tag for each version of the DLL. This will resolve the runtime assembly loading conflict. That's it, now we can use both versions as we please.

How do I change the reference version in Visual Studio?

Open the project in Visual Studio. Right-click on the project's References folder and select Add Reference to open the Add Reference dialog box. Locate the new assembly version in the Add Reference dialog for each Infragistics assembly listed in your References folder.


1 Answers

You've got two problems. First one is getting your app to compile. You've got two assembly references that contain types with the same namespace name and type name, the compiler won't know which one to choose. You solve that by using "extern alias", it lets you rename the namespace of one of the assemblies. Review this question for further help.

The second problem is the one you ask about. Without the GAC, you need to help the CLR finding the correct DLL. You must put the DLLs in a separate directory, say a subdirectory of your build directory, so that the CLR cannot find them. Use a post build event to create this directory and copy the DLLs into them. Give them distinct names.

Then implement the AppDomain.CurrentDomain.AssemblyResolve event. The CLR will fire it when it cannot find the DLLs you've hidden. Use Assembly.LoadFrom() to load and return the assembly it asks for. The e.Name property has the full assembly name, use the AssemblyName class to parse that string and retrieve the Version property.

like image 82
Hans Passant Avatar answered Sep 30 '22 09:09

Hans Passant