Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
                        }
                    }
                }
            }
        }
    }
}
like image 381
Tom Atwood Avatar asked Feb 16 '26 15:02

Tom Atwood


1 Answers

I do it like this:

 static HashSet<string> BuildDependency(AssemblyDefinition ass, MethodReference method)
        {
            var types = ass.Modules.SelectMany(m => m.GetTypes()).ToArray();
            var r = new HashSet<string>();

            DrillDownDependency(types, method,0,r);

            return r;
        }

    static void DrillDownDependency(TypeDefinition[] allTypes, MethodReference method, int depth, HashSet<string> result)
    {
        foreach (var type in allTypes)
        {
            foreach (var m in type.Methods)
            {
                if (m.HasBody &&
                    m.Body.Instructions.Any(il =>
                    {
                        if (il.OpCode == Mono.Cecil.Cil.OpCodes.Call)
                        {
                            var mRef = il.Operand as MethodReference;
                            if (mRef != null && string.Equals(mRef.FullName,method.FullName,StringComparison.InvariantCultureIgnoreCase))
                            {
                                return true;
                            }
                        }
                        return false;
                    }))
                {
                    result.Add(new string('\t', depth) + m.FullName);
                    DrillDownDependency(allTypes,m,++depth, result);
                }
            }
        }
    }
like image 105
Famos Avatar answered Feb 19 '26 05:02

Famos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!