1

I am trying to map out the dependency matrix for a collection of assemblies including what dependency methods are used where. The basic DLL dependency matrix was easy but I am finding it difficult to get the method mapping. The tool I have been using is jbevain MethodBaseRocks.cs.

The dependencies of the assembly I want to parse are being loaded according to AppDomain.CurrentDomain.GetAssemblies() but I am getting FileNotFoundException and ReflectionTypeLoadExceptions.

  • Is there a correct way to load referenced assemblies?
    • I have tried LoadFile, LoadFrom and ReflectionOnlyLoadFrom all with the same result.
  • How do I get the types for methods that use a references Type?
    • I can step around the ReflectionTypeLoadExceptions error with the answer from here but these methods are the exact ones I want to map

TestLibraryA.dll

namespace TestLibraryA
{
    public class TestClassA
    {
       public int DoStuff(int a, int b)
       {
          return a + b;
       }
    }
}

TestLibaryB.dll

using TestLibraryA;
namespace TestLibraryB
{
    public class TestClassB
    {
       public int DoStuffAgain()
       {
          TestClassA obj = new TestClassA();
          int ans = obj.DoStuff(3, 5);
          return ans;
       }

       public TestClassA DoOtherStuff()
       {
          TestClassA result = new TestClassA();
          return result;
       }
    }
}

Parser Code Application

public List<string> GetMethods()
{
    List<string> result = new List<string> { };
    Assembly dependencyAssembly = Assembly.LoadFile("TestLibraryA.dll");
    Assembly targetAssembly = Assembly.LoadFile("TestLibaryB.dll");

    Type[] types = targetAssembly.GetTypes();
    // ReflectionTypeLoadExceptions thrown if a dependency type is used
    // NB Not demo'ed in this example

    foreach(var type in types)
    {
        foreach(var method in type.GetMethods())
        {
            // With the above DoStuffAgain() method is returned but DoOtherStuff() is not

            var instructions = MethodBodyReader.GetInstructions(method);
            // FileNotFoundException thrown saying TestLibraryA.dll not loaded
            // the line throwing the error is 
            //  MethodBodyReader(method)
            //    this.body = method.GetMethodBody();

            foreach (var instruction in instructions)
            {
                MethodInfo methodInfo = instruction.Operand as MethodInfo;
                if (methodInfo != null)
                {
                    result.Add(methodInfo.DeclaringType.FullName + "." + methodInfo.Name);
                }
            }
        }
    }
    return result;
}
Community
  • 1
  • 1
KAHartle
  • 125
  • 1
  • 6
  • 1
    As shown in the code you can get to the IL instructions of all methods and thus discover the types and members being used. – Lasse V. Karlsen Aug 07 '15 at 08:06
  • Can you formulate your question with namespaces rather than assemblies? Like: "what types of namespace B are used in namespace A"? If so then there's a tool that can help you with that called NsDepCop. – Vizu Aug 07 '15 at 12:47
  • @MickyDuncan You may want to read up on [jb evain](http://stackoverflow.com/users/36702/jb-evain) work. He has posted a bit about his library. – KAHartle Aug 07 '15 at 14:02
  • @Vizu NsDepCop looks to be for managing Namespaces not the inspecting the methods in binaries. – KAHartle Aug 07 '15 at 14:07
  • @KAHartle thanks, I will :) –  Aug 07 '15 at 14:11
  • Oops sorry, I was totally off-track I wrongly assumed that you have the source of the assemblies. Still, I gave your code a try and it works with a few modifications. I changed Assembly.LoadFile to Assembly.LoadFrom and gave the full path of the assemblies. And I changed type.GetMethods() to type.GetMethods().Where(i=>i.DeclaringType == type) so inherited methods dont get returned. And it returned: TestLibraryA.TestClassA.DoStuff -- is this what you expected? – Vizu Aug 07 '15 at 14:39

1 Answers1

0

To avoid the exception you need to add the code below before you load TestLibaryB

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    if (args.Name == "TestLibaryA...")
    {
        return Assembly.LoadFrom("TestLibaryA's Path");
    }
    return null;
}
Edgar
  • 273
  • 1
  • 3
  • 12