在Visual Studio中添加 – 如何检索文本选择对象的属性(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- or-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
我发现这可能会有所帮助:(代码是超出我的技能但是) 在创建对象时自动生成属性