ISelectionContainer.GetObjects 返回的对象的类型是什么?

发布于 2024-12-18 12:36:30 字数 1174 浏览 2 评论 0原文

我正在为 VS2010 编写一个扩展,我的包实现了 IVsSelectionEvents 来跟踪解决方案资源管理器中的选择:

    public int OnSelectionChanged(
        IVsHierarchy pHierOld,
        uint itemidOld,
        IVsMultiItemSelect pMISOld,
        ISelectionContainer pSCOld,
        IVsHierarchy pHierNew,
        uint itemidNew,
        IVsMultiItemSelect pMISNew,
        ISelectionContainer pSCNew)
    {
        if (pSCNew != null)
        {
            uint count;
            pSCNew.CountObjects((uint) Constants.GETOBJS_SELECTED, out count);
            object[] selectedObjects = new object[count];
            pSCNew.GetObjects((uint) Constants.GETOBJS_SELECTED, count, selectedObjects);

            // What is the type of this object?
            var selectedItem = selectedObjects[0] as ???;

        }
        return VSConstants.S_OK;
    }

GetObjects 填充了一个对象数组,但是我不知道不知道这些对象的实际类型是什么GetType 返回System.__ComObject,这不是很有帮助。我假设这些对象实现了一个已知的接口,但我如何知道是哪一个?这似乎没有在任何地方记录...目前我正在使用 dynamic 来访问对象的属性,但这并不理想,而且无论如何我不知道哪些方法是由这个对象(我只能在“监视”窗口中看到属性)

有谁知道解决方案资源管理器中的项目项实现了哪个接口

I'm writing an extension for VS2010, and my package implements IVsSelectionEvents to track the selection in the solution explorer:

    public int OnSelectionChanged(
        IVsHierarchy pHierOld,
        uint itemidOld,
        IVsMultiItemSelect pMISOld,
        ISelectionContainer pSCOld,
        IVsHierarchy pHierNew,
        uint itemidNew,
        IVsMultiItemSelect pMISNew,
        ISelectionContainer pSCNew)
    {
        if (pSCNew != null)
        {
            uint count;
            pSCNew.CountObjects((uint) Constants.GETOBJS_SELECTED, out count);
            object[] selectedObjects = new object[count];
            pSCNew.GetObjects((uint) Constants.GETOBJS_SELECTED, count, selectedObjects);

            // What is the type of this object?
            var selectedItem = selectedObjects[0] as ???;

        }
        return VSConstants.S_OK;
    }

GetObjects fills an array of objects, but I don't know what is the actual type of these objects. GetType returns System.__ComObject, which is not very helpful. I assume these objects implement a known interface, but how do I know which one? This doesn't seem to be documented anywhere... Currently I'm using dynamic to access the properties of the object, but it's not ideal, and anyway I don't know which methods are exposed by this object (I can only see the properties in the Watch window)

Does anyone know which interface is implemented by project items in the solution explorer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗琴音 2024-12-25 12:36:30

好的,看起来 GetObjects 返回的项目的类型为 文件属性。但无论如何,我找到了一种更简单的方法,使用 EnvDTE 代替...... 可以使用以下方法从层次结构和项目 ID 中检索 EnvDTE.ProjectItem 对象:

    private static ProjectItem GetProjectItem(IVsHierarchy hierarchy, uint itemId)
    {
        object o;
        if (hierarchy.GetProperty(itemId, (int)__VSHPROPID.VSHPROPID_ExtObject, out o) == VSConstants.S_OK)
        {
            return o as ProjectItem;
        }
        return null;
    }

OK, so it seems that the items returned by GetObjects are of type FileProperties. But anyway, I found a much easier approach, using EnvDTE instead... An EnvDTE.ProjectItem object can be retrieved from the hierarchy and item id with this method:

    private static ProjectItem GetProjectItem(IVsHierarchy hierarchy, uint itemId)
    {
        object o;
        if (hierarchy.GetProperty(itemId, (int)__VSHPROPID.VSHPROPID_ExtObject, out o) == VSConstants.S_OK)
        {
            return o as ProjectItem;
        }
        return null;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文