December 2007
Intermediate to advanced
896 pages
19h 57m
English
You need to determine each assembly imported by a particular assembly. This information can show you if this assembly is using one or more of your assemblies or if your assembly is using another specific assembly.
Use the Assembly.GetReferencedAssemblies method, as shown in Example 13-1, to obtain the imported assemblies of an assembly.
Example 13-1. Using the Assembly.GetReferencedAssemblies method
using System;
using System.Reflection;
using System.Collections.Specialized
public static void BuildDependentAssemblyList(string path,
StringCollection assemblies)
{
// maintain a list of assemblies the original one needs
if(assemblies == null)
assemblies = new StringCollection();
// have we already seen this one?
if(assemblies.Contains(path)==true)
return;
Assembly asm = null;
// look for common path delimiters in the string
// to see if it is a name or a path
if ((path.IndexOf("\", 0, path.Length, StringComparison.Ordinal) != -1)
(path.IndexOf("/" 0, path.Length, StringComparison.Ordinal) != -1))
{
// load the assembly from a path
asm = Assembly.LoadFrom(path);
}
else
{
//try as assembly name
asm = Assembly.Load(path);
}
// add the assembly to the list
if (asm != null)
{
assemblies.Add(path);
}
// get the referenced assemblies
AssemblyName[] imports = asm.GetReferencedAssemblies(); // iterate foreach (AssemblyName asmName in imports) { // now recursively call this assembly to get the new modules // it references BuildDependentAssemblyList(asmName.FullName, ...Read now
Unlock full access