向对象管理器注册库

使用符号浏览工具(如类视图、对象浏览器调用浏览器查找符号结果),可以在项目或外部组件中查看符号。 符号包括命名空间、类、接口、方法和其他语言元素。 库跟踪这些符号,并将其公开给 Visual Studio 对象管理器,该管理器使用数据填充工具。

对象管理器跟踪所有可用库。 每个库都必须向对象管理器注册,然后才能为符号浏览工具提供符号。

通常,在 VSPackage 加载时注册库。 但是,可以根据需要在另一时间完成。 VSPackage 关闭时注销库。

若要注册库,请使用 RegisterLibrary 该方法。 对于托管代码库,请使用 RegisterSimpleLibrary 该方法。

若要注销库,请使用 UnregisterLibrary 该方法。

若要获取对对象管理器的引用, IVsObjectManager2请将 SVsObjectManager 服务 ID 传递给 GetService 方法。

向对象管理器注册和注销库

向对象管理器注册库

  1. 创建库。

    private CallBrowser.Library m_CallBrowserLibrary = null;
    private uint m_nLibraryCookie = 0;
    // Create Library.
    m_CallBrowserLibrary = new CallBrowser.Library();
    
    
  2. 获取对类型的对象的 IVsObjectManager2 引用并调用 RegisterSimpleLibrary 该方法。

    private void RegisterLibrary()
    {
        if (m_nLibraryCookie != 0)
            throw new Exception("Library already registered with Object Manager");
    
        // Obtain a reference to IVsObjectManager2 type object.
        IVsObjectManager2 objManager =
                          GetService(typeof(SVsObjectManager)) as IVsObjectManager2;
        if (objManager == null)
            throw new NullReferenceException("GetService failed for SVsObjectManager");
    
        try
        {
            int hr =
                objManager.RegisterSimpleLibrary(m_CallBrowserLibrary,
                                                 out m_nLibraryCookie);
                Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(hr);
        }
        catch (Exception e)
        {
            // Code to handle any CLS-compliant exception.
            Trace.WriteLine(e.Message);
            throw;
        }
    }
    
    

使用对象管理器注销库

  1. 获取对类型的对象的 IVsObjectManager2 引用并调用 UnregisterLibrary 该方法。

    private void UnregisterLibrary()
    {
        if (m_nLibraryCookie != 0)
        {
            // Obtain a reference to IVsObjectManager2 type object.
            IVsObjectManager2 objManager = GetService(typeof(SVsObjectManager)) as IVsObjectManager2;
            if (objManager == null)
                throw new NullReferenceException("GetService failed for SVsObjectManager");
    
            try
            {
                objManager.UnregisterLibrary(m_nLibraryCookie);
            }
            catch (Exception e)
            {
                // Code to handle any CLS-compliant exception.
                Trace.WriteLine(e.Message);
                throw;
            }
            finally
            {
                m_nLibraryCookie = 0;
            }
        }
    }