以编程方式找到一个函数的所有引用递归递归、一个函数、方式

2023-09-04 23:12:17 作者:凉汐

如何以编程方式搜索code,以确定所有,其中一个方法就是所谓的地方呢?是否有工具或API,可以找到一个方法的所有在整个解决方案的参考(类似于使用查找所有引用)?我需要以编程方式做到这一点,这样我可以查找引用递归。

How can I programmatically search code to determine all the places where a method is called? Is there a tool or an API that can find all the references of a method in an entire solution (similar to using Find All References)? I need to do this programmatically so that I can find references recursively.

推荐答案

我看了之前相同的解决方案,现在我有一些想法。可能有2种方式来实现这一点。一种方法是使用VSPackage的或加载(我没有尝试)。样品code是在这里:

I looked for the same solution before and now I have some idea. There may be 2 ways to achieve this. One way is use VSPackage or Add-in (which I didn't try). Sample code is here:

        IVsObjectSearch search = GetService(typeof(SVsObjectSearch)) as IVsObjectSearch;
        IVsObjectList ob;
        VSOBSEARCHCRITERIA c = new VSOBSEARCHCRITERIA();
        c.szName = ""; //fill in the method name here
        c.eSrchType = VSOBSEARCHTYPE.SO_ENTIREWORD;
        c.grfOptions = (uint)_VSOBSEARCHOPTIONS.VSOBSO_CASESENSITIVE;
        VSOBSEARCHCRITERIA[] carr = new VSOBSEARCHCRITERIA[] { c };
        int retval = search.Find((uint)__VSOBSEARCHFLAGS.VSOSF_EXPANDREFS, carr, out ob);

在VS2008中存在的OB将永远是空的错误,但现在它在VS2010 +工作。如何处理这个IVsObjectList对象是另一回事,我还在努力找出来,但应该有一种方法。

In VS2008 there was a bug that ob would always be null, but now it's working in VS2010+. How to deal with this IVsObjectList object is another thing which I'm still trying to find out, but there should be a way.

另一种方法我可以找出是使用一些静态code的分析工具。我曾经试图通过自己使用ANTLR项目和C#语法,我发现从codePLEX建立一个解析器。我有一些成就,但我没有时间来完成的。从理论上讲,这应该工作,但它不是一件容易的事。

The other method I can figure out is to use some static code analysis tool. I once tried to build a parser by myself using the ANTLR project and the C# grammar I found from Codeplex. I had some achievement but I didn't have time to finish that. Theoretically speaking this should work, but it's not an easy task.

现在也许罗斯林是另一种方法。我还没有尝试过,但。

Now maybe Roslyn is another approach. I haven't tried that, though.

希望这有助于。

 
精彩推荐