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); }