internal static ComReferenceNode AddComReference(ProjectNode project, VSCOMPONENTSELECTORDATA selectorData) { // Get the ReferenceContainerNode for this project. IReferenceContainer container = project.GetReferenceContainer(); container.AddReferenceFromSelectorData(selectorData); // Now find the refererence added. ReferenceContainerNode containerNode = container as ReferenceContainerNode; for (HierarchyNode n = containerNode.FirstChild; n != null; n = n.NextSibling) { if (n is ComReferenceNode) { ComReferenceNode refererenceNode = n as ComReferenceNode; // We check if the name is the same and the type guid is the same if (refererenceNode.TypeGuid == selectorData.guidTypeLibrary && String.Compare(refererenceNode.Caption, selectorData.bstrTitle, StringComparison.OrdinalIgnoreCase) == 0) { return(refererenceNode); } } } throw new InvalidOperationException("The Com Refererence added cannot be found"); }
public override int OnAfterRenameProject(IVsHierarchy hierarchy) { if (hierarchy == null) { return(VSConstants.E_INVALIDARG); } try { List <ProjectReferenceNode> projectReferences = this.GetProjectReferencesContainingThisProject(hierarchy); // Collect data that is needed to initialize the new project reference node. string projectRef; ErrorHandler.ThrowOnFailure(this.Solution.GetProjrefOfProject(hierarchy, out projectRef)); object nameAsObject; ErrorHandler.ThrowOnFailure(hierarchy.GetProperty(VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_Name, out nameAsObject)); string projectName = (string)nameAsObject; string projectPath = String.Empty; IVsProject3 project = hierarchy as IVsProject3; if (project != null) { ErrorHandler.ThrowOnFailure(project.GetMkDocument(VSConstants.VSITEMID_ROOT, out projectPath)); projectPath = Path.GetDirectoryName(projectPath); } // Remove and re add the node. foreach (ProjectReferenceNode projectReference in projectReferences) { ProjectNode projectMgr = projectReference.ProjectManager; projectReference.Remove(false); IReferenceContainer refContainer = projectMgr.GetReferenceContainer(); if (refContainer == null) { continue; } VSCOMPONENTSELECTORDATA selectorData = new VSCOMPONENTSELECTORDATA(); selectorData.type = VSCOMPONENTTYPE.VSCOMPONENTTYPE_Project; selectorData.bstrTitle = projectName; selectorData.bstrFile = projectPath; selectorData.bstrProjRef = projectRef; refContainer.AddReferenceFromSelectorData(selectorData); } } catch (COMException e) { return(e.ErrorCode); } return(VSConstants.S_OK); }
/// <summary> /// This is overridden to handle file references correctly when added to the project /// </summary> /// <inheritdoc /> public override int AddComponent(VSADDCOMPOPERATION dwAddCompOperation, uint cComponents, IntPtr[] rgpcsdComponents, IntPtr hwndDialog, VSADDCOMPRESULT[] pResult) { if (rgpcsdComponents == null || pResult == null) { return(VSConstants.E_FAIL); } // Initialize the out parameter pResult[0] = VSADDCOMPRESULT.ADDCOMPRESULT_Success; IReferenceContainer references = GetReferenceContainer(); if (null == references) { // This project does not support references or the reference container was not created. // In both cases this operation is not supported. return(VSConstants.E_NOTIMPL); } for (int cCount = 0; cCount < cComponents; cCount++) { VSCOMPONENTSELECTORDATA selectorData = new VSCOMPONENTSELECTORDATA(); IntPtr ptr = rgpcsdComponents[cCount]; selectorData = (VSCOMPONENTSELECTORDATA)Marshal.PtrToStructure(ptr, typeof(VSCOMPONENTSELECTORDATA)); var node = references.AddReferenceFromSelectorData(selectorData); if (node == null) { // Skip further processing since a reference has to be added pResult[0] = VSADDCOMPRESULT.ADDCOMPRESULT_Failure; return(VSConstants.S_OK); } // If it's a file, get rid of the Name and AssemblyName metadata and add the HintPath metadata. // If not, when the project is opened the next time, the reference will appear as missing // if it isn't in the GAC. if (node != null && selectorData.type == VSCOMPONENTTYPE.VSCOMPONENTTYPE_File) { string hintPath = selectorData.bstrFile; if (Path.IsPathRooted(hintPath)) { hintPath = PackageUtilities.GetPathDistance(this.ProjectMgr.BaseURI.Uri, new Uri(hintPath)); } node.ItemNode.SetMetadata(ProjectFileConstants.Name, null); node.ItemNode.SetMetadata(ProjectFileConstants.AssemblyName, null); node.ItemNode.SetMetadata(ProjectFileConstants.HintPath, hintPath); } } return(VSConstants.S_OK); }
internal static AssemblyReferenceNode AddAssemblyReference(ProjectNode project, string assemblyReference) { VSCOMPONENTSELECTORDATA selectorData = new VSCOMPONENTSELECTORDATA(); selectorData.bstrFile = assemblyReference; selectorData.bstrTitle = Path.GetFileNameWithoutExtension(assemblyReference); selectorData.type = VSCOMPONENTTYPE.VSCOMPONENTTYPE_File; // Get the ReferenceContainerNode for this project. IReferenceContainer container = project.GetReferenceContainer(); container.AddReferenceFromSelectorData(selectorData); return((AssemblyReferenceNode)((ReferenceContainerNode)container).FindChild(assemblyReference)); }
internal static AssemblyReferenceNode AddAssemblyReference(ProjectNode project, string assemblyReference) { VSCOMPONENTSELECTORDATA selectorData = new VSCOMPONENTSELECTORDATA(); selectorData.bstrFile = assemblyReference; selectorData.bstrTitle = Path.GetFileNameWithoutExtension(assemblyReference); selectorData.type = VSCOMPONENTTYPE.VSCOMPONENTTYPE_File; // Get the ReferenceContainerNode for this project. IReferenceContainer container = project.GetReferenceContainer(); container.AddReferenceFromSelectorData(selectorData); MethodInfo mi = typeof(ReferenceContainerNode).GetMethod("FindChild", BindingFlags.NonPublic | BindingFlags.Instance); return(mi.Invoke(container, new object[] { assemblyReference }) as AssemblyReferenceNode); }
internal static ProjectReferenceNode AddProjectReference(ProjectNode project, ProjectNode projectReference) { VSCOMPONENTSELECTORDATA selectorData = new VSCOMPONENTSELECTORDATA(); selectorData.bstrFile = projectReference.ProjectFolder; IVsSolution solution = (IVsSolution)project.Site.GetService(typeof(IVsSolution)); solution.GetProjrefOfItem(projectReference, VSConstants.VSITEMID_ROOT, out selectorData.bstrProjRef); selectorData.bstrTitle = projectReference.Caption; selectorData.type = VSCOMPONENTTYPE.VSCOMPONENTTYPE_Project; // Get the ReferenceContainerNode for this project. IReferenceContainer container = project.GetReferenceContainer(); container.AddReferenceFromSelectorData(selectorData); return((ProjectReferenceNode)((ReferenceContainerNode)container).FindChild(projectReference.GetMKDocument())); }
internal static ProjectReferenceNode AddProjectReference(ProjectNode project, ProjectNode projectReference) { VSCOMPONENTSELECTORDATA selectorData = new VSCOMPONENTSELECTORDATA(); selectorData.bstrFile = projectReference.ProjectFolder; IVsSolution solution = (IVsSolution)project.Site.GetService(typeof(IVsSolution)); solution.GetProjrefOfItem(projectReference, VSConstants.VSITEMID_ROOT, out selectorData.bstrProjRef); selectorData.bstrTitle = projectReference.Caption; selectorData.type = VSCOMPONENTTYPE.VSCOMPONENTTYPE_Project; // Get the ReferenceContainerNode for this project. IReferenceContainer container = project.GetReferenceContainer(); container.AddReferenceFromSelectorData(selectorData); MethodInfo mi = typeof(ReferenceContainerNode).GetMethod("FindChild", BindingFlags.NonPublic | BindingFlags.Instance); return(mi.Invoke(container, new object[] { projectReference.GetMkDocument() }) as ProjectReferenceNode); }