在VS 2015扩展中,如何在解决方案资源管理器中获取选定的对象? [英] In a VS 2015 extension, how can I get the selected object in the Solution Explorer?

查看:114
本文介绍了在VS 2015扩展中,如何在解决方案资源管理器中获取选定的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣为仅选择一个项目的当前选择获取一个Project或ProjectItem(例如,但不限于这两个)。

I'm interested in getting a Project or ProjectItem (as examples, not limited to those two) for the current selection where only a single item is selected.

大多数人似乎正在使用 IVsMonitorSelection 来获取 IVSHierarchy ,然后使用以下内容来获取所选项目的对象(如果选择了单个项目):

Most people seem to be using IVsMonitorSelection to get an IVSHierarchy and then use the following to get the object for the selected item (in case of a single item being selected):

var monitorSelection = (IVsMonitorSelection) Package.GetGlobalService(typeof(IVsMonitorSelection));

IntPtr hierarchyPointer, selectionContainerPointer;
uint projectItemId;
IVsMultiItemSelect multiItemSelect;

monitorSelection.GetCurrentSelection(out hierarchyPointer, out projectItemId, out multiItemSelect, out selectionContainerPointer);

var hierarchy = (IVsHierarchy) Marshal.GetObjectForIUnknown(hierarchyPointer);

Marshal.Release(hierarchyPointer);
Marshal.Release(selectionContainerPointer);

object o;

hierarchy.GetProperty((uint) projectItemId, (int) __VSHPROPID.VSHPROPID_ExtObject, out o);

但是, GetProperty 在此处返回E_NOTIMPL。我使用了错误的参数吗?

However, GetProperty returns E_NOTIMPL here. Am I using the wrong parameters? Is there an alternative solution perhaps?

推荐答案

根据谢尔盖(Sergey)的回答,我找到了dte.SelectedItems,甚至更多。强类型,不需要强制转换为包含UIHierarchy
项目的数组。

Based on the answer from Sergey, I found dte.SelectedItems, which is even "more strongly-typed" and does not require casting to an array containing UIHierarchy items.

现在的结果是:

dte.SelectedItems.Item(1).ProjectItem

这篇关于在VS 2015扩展中,如何在解决方案资源管理器中获取选定的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆