Search code examples
c#.netgac

How can we access a binary from GAC irrespective of .Net Framework


Initially, we were working on .Net Framework 3.5. Hence the GAC path was hardcoded in code as

C:\Windows\assembly\GAC_64\{0}\1.0.0.0__008145f79b9aec14\{0}.dll

Later we migrated to .Net Framework 4.0. Now the required path should be

C:\Windows\Microsoft.NET\assembly\GAC_64\{0}\v4.0_1.0.0.0__008145f79b9aec14\{0}.dll

Currently the problem can be fixed by putting the second path in my code. But when Microsoft releases its next set of frameworks, it might fail.

So i wanted to know if there are any ways in which we can access GAC binaries independent of .Net Framework.

Any comments on this will be helpful. Thank you


Solution

  • If you want to load an assembly from the GAC, load it by using its fully qualified name:

    Assembly.Load("SampleAssembly, Version=1.0.2004.0, Culture=neutral, PublicKeyToken=8744b20f8da049e3");
    

    If it's found in the GAC, it will be loaded from there.

    You should never ever hardcode the path of the GAC anywhere. It could change at any time, even between service packs or patches, and it's an implementation detail that you cannot rely on.