private static T GetOpenedDocData <T>(Document document) where T : class { var rdt = new RunningDocumentTable(ServiceProvider.GlobalProvider); var documentInfo = rdt.FirstOrDefault(info => info.Moniker.Equals(document.FullName, StringComparison.OrdinalIgnoreCase)); return(documentInfo.DocData as T); }
/// <summary> /// Gets the designer from the specified file if it is open in its default editor. /// </summary> /// <param name="fileName">The physical path to the file to open.</param> public static T GetDesignerData <T>(string fileName) where T : class { var dte = ServiceProvider.GlobalProvider.GetService <SDTE, DTE>(); var rdt = new RunningDocumentTable(ServiceProvider.GlobalProvider); var documentInfo = rdt.FirstOrDefault(info => info.Moniker.Equals(fileName, StringComparison.OrdinalIgnoreCase)); if (!string.IsNullOrEmpty(documentInfo.Moniker)) { ActivateDocument(documentInfo.Moniker); var docData = documentInfo.DocData as T; if (docData == null) { // Close file is not opened in 'designer' view var projectItem = dte.Solution.FindProjectItem(documentInfo.Moniker); projectItem.Document.Close(vsSaveChanges.vsSaveChangesYes); // Open in designer mode (invisibly) docData = GetOpenedDocData <T>(OpenDesigner(fileName, false)); } return(docData); } return(null); }