I have scratched my head for over a day on this:
Essentially I am attempting to build an Add-In for Visual Studio 2012 that does the following:
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:
BEFORE:
eg. (Consider myPerson is selected)
int CountPerson(Person myPerson)
{
*myPerson*
}
AFTER:
int CountPerson(Person myPerson)
{
myPerson.Name
myPerson.Surname
myPerson.Age
}
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.
I will live with typing "Person" instead of |myPerson" if that will make things easier.
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/
They may help you with helping me?