Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get dependent assemblies?

Is there a way to get all assemblies that depend on a given assembly?

Pseudo:

Assembly a = GetAssembly();
var dependants = a.GetDependants();
like image 698
Shimmy Weitzhandler Avatar asked Jan 13 '12 10:01

Shimmy Weitzhandler


People also ask

How to find dependent assemblies?

Select one or more assemblies in the Assembly Explorer window, right click the selection and choose Show Assembly Dependency Diagram in the context menu . The diagram will include all selected assemblies as well as all assemblies referenced from them. References between assemblies are represented with arrows.

What are the possible ways to obtain the reference of an Assembly object?

Solution 1Assembly. GetReferencedAssemblies()[^]. Basically you'll construct an Assembly object to represent the DLL you want to get the references of, and then call the GetReferencedAssemblies method and see if any of the entries refer to the specific POS DLL.

What is Assembly C#?

C# Assembly is a standard library developed for . NET. Common Language Runtime, CLR, MSIL, Microsoft Intermediate Language, Just In Time Compilers, JIT, Framework Class Library, FCL, Common Language Specification, CLS, Common Type System, CTS, Garbage Collector, GC.


2 Answers

If you wish to find the dependent assemblies from the current application domain, you could use something like the GetDependentAssemblies function defined below:

private IEnumerable<Assembly> GetDependentAssemblies(Assembly analyzedAssembly)
{
    return AppDomain.CurrentDomain.GetAssemblies()
        .Where(a => GetNamesOfAssembliesReferencedBy(a)
                            .Contains(analyzedAssembly.FullName));
}

public IEnumerable<string> GetNamesOfAssembliesReferencedBy(Assembly assembly)
{
    return assembly.GetReferencedAssemblies()
        .Select(assemblyName => assemblyName.FullName);
}

The analyzedAssembly parameter represents the assembly for which you want to find all the dependents.

like image 179
Cristian Lupascu Avatar answered Oct 06 '22 00:10

Cristian Lupascu


Programatically, you can use Mono.Cecil to do this.

Something like this (note this won't work if the debugger is attached - e.g. if you run it from inside VS itself):

public static IEnumerable<string> GetDependentAssembly(string assemblyFilePath)
{
   //On my box, once I'd installed Mono, Mono.Cecil could be found at: 
   //C:\Program Files (x86)\Mono-2.10.8\lib\mono\gac\Mono.Cecil\0.9.4.0__0738eb9f132ed756\Mono.Cecil.dll
   var assembly = AssemblyDefinition.ReadAssembly(assemblyFilePath);
   return assembly.MainModule.AssemblyReferences.Select(reference => reference.FullName);
}

If you don't need to do this programatically, then NDepend or Reflector can give you this information.

like image 20
Rob Levine Avatar answered Oct 06 '22 00:10

Rob Levine