简体   繁体   中英

How to find uses of a method in a second assembly?

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;
}

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM