How to reference assembly from GAC?

Learner picture Learner · Feb 17, 2011 · Viewed 9.9k times · Source

I have installed the strong named assembly TestReflection into the GAC (I am using .NET 4.0 and VS 2010).

Different versions of the TestReflection DLL are in GAC of .NET 4.0 (C:\WINDOWS\Microsoft.NET\assembly\GAC_32\TestReflection\), however, the assembly does not appear in the "Project" -> "Add reference" box of VS 2010.

How can I refer to my assembly deployed in GAC at design time from another project?

This page says that:

You cannot add references from the Global Assembly Cache (GAC), as it is strictly part of the run-time environment.

Referring to this statement, I would like to know how to make your project's DLL shared assembly for other consumers if it's the requirement?

Answer

Peter Hansen picture Peter Hansen · Feb 17, 2011

The dll's shown in the .Net tab of the "Add references" dialog are not actually the ones registered in the GAC. They are found by searching a few paths on your filesystem.

The paths being searched are located by Visual Studio by looking up the following registry entries:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NetFramework\{Version}\AssemblyFoldersEx\

There should be some keys added there already, so if you want your own dll to show up on the .Net tab, you can add it to one of the folders defined there. You could also add a new registry key pointing to a custom folder, which would only contain your own dll's.

The GAC is only meant for loading assemblies at runtime after your application has been deployed, so I don't think you should use it while developing. When you deploy your app, make sure to set "Copy local" to false on your reference so the dll won't be copied to the bin folder, and then install it into the GAC and it will be loaded from there instead.