Recipe13.1.Listing Referenced Assemblies


Recipe 13.1. Listing Referenced Assemblies

Problem

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.

Solution

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 string[] BuildDependentAssemblyList(string path,                                                 List<string> assemblies) {     // Maintain a list of assemblies the original one needs.     if (assemblies == null)         assemblies = new List<string>();     // Have we already seen this one?     if (assemblies.Contains(path) == true)         return (new string[0]);     Assembly asm = null;     // Look for common path delimiters in the string     // to see if it is a name or a path.     if ((path.IndexOf(Path.DirectorySeparatorChar, 0, path.Length) != -1) ||         (path.IndexOf(Path.AltDirectorySeparatorChar, 0, path.Length) != -1))     {         // Load the assembly from a path.         asm = Assembly.ReflectionOnlyLoadFrom(path);     }     else     {         // Try as assembly name.         asm = Assembly.ReflectionOnlyLoad(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, assemblies);     }     string[] temp = new string[assemblies.Count];     assemblies.CopyTo(temp, 0);     return (temp);  } 

This code returns a string[] containing the original assembly, all imported assemblies, and the dependent assemblies of the imported assemblies.

If you ran this method against the assembly C:\CSharpRecipes\bin\Debug\CSharpRecipes.exe, you'd get the following dependency tree:

 C:\CSharpRecipes\bin\Debug\CSharpRecipes.exe mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Configuration, Version=2.0.3600.0, Culture=neutral,        PublicKeyToken=b03f5f7f11d50a3a System.Xml, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Security, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a System.Web.RegularExpressions, Version=2.0.3600.0, Culture=neutral,        PublicKeyToken=b03f5f7f11d50a3a System.Runtime.Serialization.Formatters.Soap, Version=2.0.3600.0, Culture=neutral,        PublicKeyToken=b03f5f7f11d50a3a 

Discussion

Obtaining the imported types in an assembly is useful in determining what assemblies another assembly is using. This knowledge can greatly aid in learning to use a new assembly. This method can also help determine dependencies between assemblies for shipping purposes.

The GetreferencedAssemblies method of the System.Reflection.Assembly class obtains a list of all the imported assemblies. This method accepts no parameters and returns an array of AssemblyName objects instead of an array of Types. The AssemblyName type is made up of members that allow access to the information about an assembly, such as the name, version, culture information, public/private key pairs, and other data.

Note that this method does not account for assemblies loaded using the Assembly.ReflectionOnlyLoad* methods, as it is inspecting for only compile-time references.

When loading assemblies for inspection using reflection, you should use the ReflectionOnlyLoad* methods. These methods do not allow you to execute code from the loaded assembly.


See Also

See the "Assembly Class" topic in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net