コード例 #1
0
        /// <summary>
        /// Gets the service provider for this item.
        /// </summary>
        public static IServiceProvider GetServiceProvider(this IVsHierarchyItem item)
        {
            Ole.IServiceProvider oleSp;
            if (item.HierarchyIdentity.IsNestedItem &&
                ErrorHandler.Succeeded(item.HierarchyIdentity.NestedHierarchy.GetSite(out oleSp)) &&
                oleSp != null)
            {
                return(new ServiceProvider(oleSp));
            }

            if (ErrorHandler.Succeeded(item.HierarchyIdentity.Hierarchy.GetSite(out oleSp)) &&
                oleSp != null)
            {
                return(new ServiceProvider(oleSp));
            }

            // Try the hierarchy root as a fallback.
            var root = item.GetRoot();

            if (root != null &&
                ErrorHandler.Succeeded(root.HierarchyIdentity.Hierarchy.GetSite(out oleSp)) &&
                oleSp != null)
            {
                return(new ServiceProvider(oleSp));
            }

            // Try the hierarchy top-most node as a fallback (this would be the solution itself)
            root = item.GetTopMost();
            if (root != null &&
                ErrorHandler.Succeeded(root.HierarchyIdentity.Hierarchy.GetSite(out oleSp)) &&
                oleSp != null)
            {
                return(new ServiceProvider(oleSp));
            }

            // Default to a global service provider provided by VS shell.
            // TODO: may require us to switch to the UI thread to get the right one.
            return(ServiceProvider.GlobalProvider);
        }