
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
754
|
Chapter 13: Reflection
13.1 Listing Referenced Assemblies
Problem
You need to determine each assembly imported by a particular assembly. This infor-
mation 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. ...