Checking .Net Assembly Dependencies

I recently wanted to automate a check on the dependencies of various DLL’s in a library, to ensure that I did not reference old versions of the general DLL’s. This was a little more of a challenge than I’d thought so let me share my experiences.

A lot of Google hits and samples told me to use the GetExportedTypes() function of the Assembly class in the System.Reflection namespace- however this functions requires the referenced DLL to actually be available in the path, as it get’s physically loaded.

Well, the solution was simple: get the depencies from the GetReferencedAssemblies() array instead.

Here’s a small sample, with a check that we haven’t accidentally put debug code in the production environment:

		static void WriteDependencies(string filename)
		{
			Assembly assembly = Assembly.LoadFrom(filename);
			AssemblyName assemblyName = assembly.GetName();
			Console.Write(assemblyName.FullName);

			foreach (object o in assembly.GetCustomAttributes(false))
				if (o is DebuggableAttribute) Console.Write(" Debug Build");

			Console.WriteLine("\nDependencies:");

			foreach (AssemblyName ans in assembly.GetReferencedAssemblies())
				Console.WriteLine(ans.FullName);
		}

Advertisements
Posted in C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s