private static int EnsureTab(ContentType documentType, Type tab)
        {
            if (!tab.IsSubclassOf(typeof(Tab)))
            {
                throw new InvalidTabTypeException(tab);
            }

            if (tab == typeof(DefaultTab))
            {
                return(0);
            }

            var tabInstance = (Tab)Activator.CreateInstance(tab);

            var documentTab = documentType.getVirtualTabs.FirstOrDefault(t => t.GetRawCaption() == tabInstance.Name);

            if (documentTab == null)
            {
                var tabId = documentType.AddVirtualTab(tabInstance.Name);
                // This doesn't always return a tab, why? Any other way?
                documentType.ClearVirtualTabs();
                documentTab = documentType.getVirtualTabs.FirstOrDefault(t => t.Id == tabId);
            }

            if (documentTab != null)
            {
                documentType.SetTabName(documentTab.Id, tabInstance.Name);
                documentType.SetTabSortOrder(documentTab.Id, tabInstance.SortOrder);
            }

            return(documentTab == null ? 0 : documentTab.Id);
        }