In Visual Studio Add In - How to Get the Properties of a Text Selector (Visual Commander)

I scratched my head over this day:

Essentially, I'm trying to create an add-in for Visual Studio 2012 that does the following:

Take the variable name that is currently selected, and find the class that is the instance, then enter veriable.property for each property in its line:

BEFORE:

eg. (Select myPerson)

int CountPerson(Person myPerson)
{
    *myPerson*
}

AFTER:

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

I asked a similar question here on stackoverflow and got the answer that I am pursuing right now. Visual Studio resets all class properties to the editor

Here is the source code:

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 just fine, except that it completely ignores the selected text and prints the properties of the current class instead. I need the class properties of the variable that I select.

"Person" "myPerson", .

, : 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/

?

+1
2

:

( 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);
}
+1

, . , :

using EnvDTE;
using EnvDTE80;
using System;

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)
                    throw new Exception("No Selection");

                EnvDTE.CodeParameter codeParam = ts.ActivePoint.CodeElement[vsCMElement.vsCMElementParameter] as EnvDTE.CodeParameter;
                if (codeParam == null)
                    throw new Exception("Not Parameter");

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

                // Move into the method and dump the properties there
                ts.FindText("{");
                ts.Insert("{" + 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);
    }
}

:

    * It doesnt work for a variable in the code only for function parameters

, : ( )

0

All Articles