共用方式為


使用物件管理員註冊程式庫

符號瀏覽工具 (例如:類別檢視物件瀏覽器呼叫瀏覽器尋找符號結果),使您能夠在專案或外部元件中檢視符號。 符號包括命名空間、類別、介面、方法和其他語言元素。 程式庫追蹤這些符號,並將其公開給 Visual Studio 物件管理員以使用資料填入工具。

物件管理員追蹤所有可用的程式庫。 每個程式庫都必須向物件管理員註冊,才能為符號瀏覽工具提供符號。

通常,您在載入 VSPackage 時會註冊程式庫。 不過,您可以視需要在其他時間完成。 當 VSPackage 關閉時,可以取消註冊程式庫。

若要註冊程式庫,請使用 RegisterLibrary 方法。 對於受控程式碼程式庫,請使用 RegisterSimpleLibrary 方法。

若要取消註冊程式庫,請使用 UnregisterLibrary 方法。

若要取得物件管理員 IVsObjectManager2 的參考,請將 SVsObjectManager 服務識別碼傳遞至 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;
            }
        }
    }