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/
?