我为此绞尽脑汁了一天多:
本质上,我正在尝试构建一个用于 Visual Studio 2012 的加载项,该加载项执行以下操作:
获取当前选择的变量名称,找到它作为实例的类,然后在其自己的行中为每个属性键入 veriable.property:
BEFORE:
例如。 (考虑选择了myPerson)
int CountPerson(Person myPerson)
{
*myPerson*
}
AFTER:
int CountPerson(Person myPerson)
{
myPerson.Name
myPerson.Surname
myPerson.Age
}
我在 stackoverflow 上问了类似的问题,并得到了我现在正在追求的答案。Visual Studio 将类的所有属性转储到编辑器中 https://stackoverflow.com/questions/25590751/visual-studio-dump-all-properties-of-class-into-editor
这是到目前为止的源代码:
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;
}
}
这工作得很好,除了它完全忽略所选文本,而是打印当前类的属性。我需要我选择的变量的类的属性。
如果输入“Person”而不是“|myPerson”会让事情变得更容易,我会接受。
我在互联网上找到了以下链接,但无法实现该逻辑:http://blogs.clariusconsulting.net/kzu/how-to-get-a-system-type-from-an-envdte-codetyperef-or-envdte-codeclass/ 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/ http://www.visualstudioextensibility.com/2008/03/06/how-do-i-get-a-system-type-from-a-type-name/
他们可以帮助你帮助我吗?