【发布时间】:2017-04-25 18:53:47
【问题描述】:
我正在处理一个清理遗留代码的项目,并且需要以编程方式查找在 .NET 4.5 服务引用(即 Reference.cs 文件)中调用某些 SOAP Web 方法的所有引用,以便我可以输出到文本文件或 Excel(基本上,与 CodeLens 功能一起列出的参考资料)。我想我会使用 Mono.Cecil 库来完成这项任务。
我有用于指定程序集和类的方法工作得很好,因为我可以打印所有要查看的方法的列表。但是对于如何获取特定方法的参考列表有什么想法吗?
// 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
}
}
}
}
}
}
}
【问题讨论】:
标签: c# .net reflection mono