Easy enough. First, let's see how to get referenced assemblies:
System.Reflection.Assembly assembly =
System.Reflection.AssemblyName[] names = assembly.GetReferencedAssemblies();
The class
<code>
System.Reflection.AssemblyName carries full identity of the assembly, including its version, see
http://msdn.microsoft.com/en-us/library/system.reflection.assemblyname.aspx[
^].
You can compare two assembly names using "==" operator. If assemblies are signed, they are compared by their world-unique names. When you need to know the path name if the referenced assembly, check
AssemblyName.CodeBase
. This property shows the path name of assemblies main
executable module. The .NET Framework and C# compiler supports assemblies composed of more that one
module, but Visual Studio supports only single-module assemblies. (Do not mix up modules with referenced assemblies. Modules are more fine-grain units than assemblies.)
Naturally, assembly file(s) cannot not contain information on other assemblies which reference it. You can only get information on assemblies referenced by a given assembly. As you need to know which assemblies reference your given assembly, you can, for example, collect all executable files (EXE, DLL or whatever else you're interested in) in some directory (possible in all file system), examine if it is a valid assembly, then examine all referenced assemblies using
System.Reflection.Assembly.GetReferencedAssemblies
. Pick up all assemblies which reference you given assembly. It can be costly operation:
string[] GetReferencingAssemblies(string referencedAssembly, string[] executableModules) {
string referencedAssemblyLo = referencedAssembly.ToLower();
System.Collections.Generic.List<string> result =
new System.Collections.Generic.List<string>();
foreach (string executableModule in executableModules) {
try {
System.Reflection.Assembly assembly =
System.Reflection.Assembly.LoadFrom(executableModule);
System.Reflection.AssemblyName[] names = assembly.GetReferencedAssemblies();
if (Array.FindIndex(
names,
new Predicate<system.reflection.assemblyname>((name) => {
return name.CodeBase.ToLower() == referencedAssemblyLo;
}))>=0)
result.Add(executableModule);
} catch { continue; }
}
return result.ToArray();
}
You can get the array of path names of the files of certain extension using
System.IO.Directory
. Be careful! It's not so easy. Do do you right, see:
Directory.Get.Files search pattern problem[
^].
—SA