![](/img/trans.png)
[英]How can I programmatically retrieve a loaded project in Visual Studio 2008?
[英]In Visual Studio Add In - How can I retrieve the properties of the text selection's object (Visual Commander)
我已经为此花了整整一天的时间:
本质上,我试图为Visual Studio 2012构建一个外接程序,该外接程序执行以下操作:
以当前选择的变量名称为例,转到找到其实例的类,然后在每个属性行上分别输入veriable.property:
之前:
例如。 (考虑选择myPerson)
int CountPerson(Person myPerson)
{
*myPerson*
}
后:
int CountPerson(Person myPerson)
{
myPerson.Name
myPerson.Surname
myPerson.Age
}
我在这里对stackoverflow提出了类似的问题,并且收到了我正在追求的答案。 Visual Studio将类的所有属性转储到编辑器中
这是到目前为止的源代码:
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-或-envdte-codeclass / http://www.visualstudioextensibility.com/2008/03/06/how-do-i-get-a-system-type-from-a-type-name/
他们可以帮助您吗?
您可以将光标设置在函数定义行中的函数参数名称上,并使用以下代码生成属性列表:
(添加对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);
}
好的,感谢上面的谢尔盖,我现在有了下面的代码,可以回答我的大部分问题:
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
我发现这可能会有所帮助:(尽管代码超出了我的技能范围) 创建对象时自动生成属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.