列出所有使用Mono.Cecil调用方法的引用 [英] List all references calling a method using Mono.Cecil

查看:258
本文介绍了列出所有使用Mono.Cecil调用方法的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目中清理遗留代码,并且需要以编程方式找到在.NET 4.5服务引用(即Reference.cs文件)中调用某些SOAP Web方法的所有引用,因此我可以输出到文本文件或Excel(基本上是,以及CodeLens功能列出的参考).我以为我会使用 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
                        }
                    }
                }
            }
        }
    }
}

推荐答案

我这样做是这样的:

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

这篇关于列出所有使用Mono.Cecil调用方法的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆