在Visual Studio加载项中-如何检索文本选择对象的属性(Visual Commander) [英] In Visual Studio Add In - How can I retrieve the properties of the text selection's object (Visual Commander)

查看:146
本文介绍了在Visual Studio加载项中-如何检索文本选择对象的属性(Visual Commander)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为此,我已经花了整整一天的时间:

I have scratched my head for over a day on this:

本质上,我正在尝试为Visual Studio 2012构建一个可执行以下操作的加载项:

Essentially I am attempting to build an Add-In for Visual Studio 2012 that does the following:

采用当前选定的变量名称,查找其实例的类,然后为每个属性单独输入veriable.property:

Take the variable name that is currently selected, go and find the class that it is an instance of, then type the veriable.property for each property on its own line:

之前:

例如。 (考虑选择myPerson)

eg. (Consider myPerson is selected)

int CountPerson(Person myPerson)
{
    *myPerson*
}

之后:

int CountPerson(Person myPerson)
{
    myPerson.Name
    myPerson.Surname
    myPerson.Age
}

我在这里对stackoverflow提出了类似的问题,并收到了我正在追求的答案。
Visual Studio将类的所有属性转储到编辑器中

I have asked a similar question here on stackoverflow, and received the answer that I am now pursuing. Visual Studio dump all properties of class into editor

这是到目前为止的源代码:

Here is the source code so far:

using EnvDTE;
using EnvDTE80;
using System;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;


public class C : VisualCommanderExt.ICommand
{
    public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package) 
    {
        EnvDTE.TextSelection ts = DTE.ActiveWindow.Selection as EnvDTE.TextSelection;
        if (ts == null)
            return;

        EnvDTE.CodeClass codeClass = ts.ActivePoint.CodeElement[vsCMElement.vsCMElementClass]   as     EnvDTE.CodeClass;
        if (codeClass == null)
            return;

        string properties = "";
        foreach (CodeElement elem in codeClass.Members)
        {
            if (elem.Kind == vsCMElement.vsCMElementProperty)
                properties += elem.Name + System.Environment.NewLine;
        }
        ts.Text = properties;   

    }
}

这很好,除了它会完全忽略选定的文本,而是打印当前类的属性。我需要选择的变量的类的属性。

This works perfectly fine, except that it completely ignores the selected text, and instead prints the properties of the current class. I need the properties of the class of the variable I am selecting.

如果可以简化事情,我会输入 Person而不是 myPerson。

I will live with typing "Person" instead of |myPerson" if that will make things easier.

我在互联网上找到了以下链接,但无法实现该逻辑:
> http://blogs.clariusconsulting.net/kzu/how-to-get-a-system-type-from-an -envdte-codetyperef-or-envdte-codeclass /
> http://www.visualstudioextensibility.com/2008/03/06/how-do-i-get-a-system-type- from-a-type-name /

I have found the following links on the internet, but was unable to implement the logic: http://blogs.clariusconsulting.net/kzu/how-to-get-a-system-type-from-an-envdte-codetyperef-or-envdte-codeclass/ http://www.visualstudioextensibility.com/2008/03/06/how-do-i-get-a-system-type-from-a-type-name/

他们可以帮助您吗?

推荐答案

您可以将光标设置在函数定义行中的函数参数名称上,并使用以下代码生成属性列表:

You can set the cursor on a function parameter name in the function definition line and generate properties list with the following code:

(添加对Micro的引用soft.VisualStudio.Shell.Design)

(add a reference to Microsoft.VisualStudio.Shell.Design)

public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package) 
{
    EnvDTE.TextSelection ts = DTE.ActiveWindow.Selection as EnvDTE.TextSelection;
    if (ts == null)
        return;

    EnvDTE.CodeParameter codeParam = ts.ActivePoint.CodeElement[vsCMElement.vsCMElementParameter] as EnvDTE.CodeParameter;
    if (codeParam == null)
        return;

    System.Type tClass = GetTypeByName(DTE, package, codeParam.Type.AsFullName);
    string properties = "";
    foreach (var p in tClass.GetProperties())
    {
            properties += codeParam.Name + "." + p.Name + System.Environment.NewLine;
    }
    System.Windows.Clipboard.SetText(properties);
    System.Windows.MessageBox.Show(properties);
}

private System.Type GetTypeByName(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package, string name)
{
    System.IServiceProvider serviceProvider = package as System.IServiceProvider;
    Microsoft.VisualStudio.Shell.Design.DynamicTypeService typeService = 
        serviceProvider.GetService(typeof(Microsoft.VisualStudio.Shell.Design.DynamicTypeService)) as
        Microsoft.VisualStudio.Shell.Design.DynamicTypeService;

    Microsoft.VisualStudio.Shell.Interop.IVsSolution sln = 
        serviceProvider.GetService(typeof(Microsoft.VisualStudio.Shell.Interop.IVsSolution)) as
        Microsoft.VisualStudio.Shell.Interop.IVsSolution;

    Microsoft.VisualStudio.Shell.Interop.IVsHierarchy hier;
    sln.GetProjectOfUniqueName(DTE.ActiveDocument.ProjectItem.ContainingProject.UniqueName, out hier);

    return typeService.GetTypeResolutionService(hier).GetType(name, true);
}

这篇关于在Visual Studio加载项中-如何检索文本选择对象的属性(Visual Commander)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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