【发布时间】:2014-09-08 12:24:06
【问题描述】:
我为此挠了一天多:
基本上我正在尝试为 Visual Studio 2012 构建一个加载项,它执行以下操作:
取当前选择的变量名,找到它作为实例的类,然后在自己的行中为每个属性键入veriable.property:
之前:
例如。 (考虑选择了 myPerson)
int CountPerson(Person myPerson)
{
*myPerson*
}
之后:
int CountPerson(Person myPerson)
{
myPerson.Name
myPerson.Surname
myPerson.Age
}
我在 stackoverflow 上问了一个类似的问题,并得到了我现在正在追求的答案。 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://www.visualstudioextensibility.com/2008/03/06/how-do-i-get-a-system-type-from-a-type-name/
他们可能会帮助你帮助我吗?
【问题讨论】:
-
您是否成功创建了扩展程序?你把它放到 Visual Studio 库了吗?
-
下面的代码是我所能得到的,我最终放弃了,没有它就活了
-
您是否介意再帮我一些忙?
-
我自己做了一个对Visual Studio的扩展,在这里我得到了当前选择所在的类名和方法名。我在GitHub上有源,它叫CopyForReview可能正在浏览它的源可以帮到你吗?
-
谢谢,会的! :)
标签: c# visual-studio visual-studio-2012 add-in