在Visual Studio中右键单击捕获方法,类,程序集名称

时间:2023-01-14 19:11:46

I'd like to write a Visual Studio Extension that captures the following information when a user right clicks a portion of code

我想编写一个Visual Studio扩展,当用户右键单击一部分代码时,它会捕获以下信息

  1. Detect if a user has clicked a method and if so get the method name

    检测用户是否单击了某个方法,如果是,则获取方法名称

  2. Retrieve the full class name of the method ie (namespace + class name)

    检索方法的完整类名,即(名称空间+类名)

  3. Retrieve the containing project's output type ie, class library etc

    检索包含项目的输出类型,即类库等

  4. The full assembly name, ie path + name

    完整的程序集名称,即路径+名称

I'm using a combination of EnvDTE object and IVsTextManager GetActiveView() method but I am unable to get all of the information I require. I've seen Test Driven do this when running unit tests inside Visual Studio via a right click so I am hoping I can achieve the same.

我正在使用EnvDTE对象和IVsTextManager GetActiveView()方法的组合,但我无法获得所需的所有信息。我已经看到Test Driven通过右键单击在Visual Studio中运行单元测试时这样做,所以我希望我能达到同样的目的。

The only thing I've been able to extract is the method name to date but it requires the method name to be selected in the IDE. EnvDTE is good for returning the project name but that's not really any good to me.

我唯一能够提取的是迄今为止的方法名称,但它需要在IDE中选择方法名称。 EnvDTE很适合返回项目名称,但这对我来说并不是很好。

var methodName = (EnvDTE.DTE)this.GetService(typeof(EnvDTE.DTE)).ActiveDocument.Object("").Selection.Text;

After this to get the class name/namespace all I have is a reference to the line that was selected, ie the method name. From here I read the selected file in the active window and read backwards from the method name to try and find the most recent occurrence of the class keyword and then namespace. It's very inelegant and clunky.

在此之后获取类名/命名空间我所拥有的是对所选行的引用,即方法名称。从这里,我在活动窗口中读取所选文件,并从方法名称向后读取,以尝试查找最近出现的class关键字,然后查找名称空间。它非常优雅和笨重。

1 个解决方案

#1


0  

You can get the method at the cursor with CodeElement:

您可以使用CodeElement在游标上获取该方法:

DTE.ActiveWindow.Selection.ActivePoint.CodeElement(vsCMElement.vsCMElementFunction);

See also Discovering Code by Using the Code Model about the CodeModel class.

另请参阅使用CodeModel类的代码模型发现代码。

#1


0  

You can get the method at the cursor with CodeElement:

您可以使用CodeElement在游标上获取该方法:

DTE.ActiveWindow.Selection.ActivePoint.CodeElement(vsCMElement.vsCMElementFunction);

See also Discovering Code by Using the Code Model about the CodeModel class.

另请参阅使用CodeModel类的代码模型发现代码。