Wednesday, 11 December 2013

.NET Subtleties: An Assembly's CodeBase versus its Location

CodeBase versus Location

The CodeBase property on the Assembly class gets the "original" location of a .NET assembly, for example, if an assembly was downloaded from the web, the CodeBase property would start with "http://".

The CodeBase property is not guaranteed to be set for assemblies in the GAC.  I repeat, the CodeBase property is not guaranteed to be set for assemblies in the GAC.

This differs from the Assembly's Location, which is where the assembly is currently at. Suzanne Cook's CLR notes are a great starting point to learn more about assemblies in a more coherent setting than just reading standard documentation.

Here's a Thing

Before you go writing code relying on the CodeBase property, take a moment to think whether your application may have the right "path discovery permissions" to actually query this property sensibly.

A Task Worth Doing

As assemblies are fundamental and unavoidable piece of the .NET ecosystem, understanding the Assembly class in System.Reflection is a task well doing.

No comments: