vendredi 4 novembre 2016

List all references calling a method using Mono.Cecil

I am working on a project to clean up legacy code and need to programmatically find all references calling certain SOAP web methods in .NET 4.5 service references (i.e. Reference.cs files) so I can output to text files or Excel (basically, the references as listed with CodeLens features). I figured I would use the Mono.Cecil library for this task.

I have the methods for the specified assemblies and classes working great as I can print a list of all the methods to review. But any thoughts on how can I get the list of references for the specific method?

// assemblyName is the file path for the specific dll   
public static void GetReferencesList(string assemblyName)
    {
        AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(assemblyName);

        foreach (ModuleDefinition module in assembly.Modules)
        {
            foreach (TypeDefinition type in module.Types)
            {
                if (type.Name.ToLowerInvariant() == "classname")
                {
                    foreach (MethodDefinition method in type.Methods)
                    {
                        if (method.Name.Substring(0, 4) != "get_" &&
                            method.Name.Substring(0, 4) != "set_" &&
                            method.Name != ".ctor" &&
                            method.Name != ".cctor" &&
                            !method.Name.Contains("Async"))
                        {
                            //Method name prints great here
                            Console.WriteLine(method.Name);

                            // Would like to collect the list of referencing calls here
                            // for later output to text files or Excel
                        }
                    }
                }
            }
        }
    }
}





Aucun commentaire:

Enregistrer un commentaire