/// <summary> /// Creates an assemby or com reference node given a slector data. /// </summary> protected virtual TEntryPointFileNode CreateFileComponent(VSCOMPONENTSELECTORDATA selectorData) { // We have a path to a file, it could be anything // First see if it is a managed assembly bool tryToCreateAnAssemblyReference = true; if (File.Exists(selectorData.bstrFile)) { try { // We should not load the assembly in the current appdomain. // If we do not do it like that and we load the assembly in the current appdomain then the assembly cannot be unloaded again. // The following problems might arose in that case. // 1. Assume that a user is extending the MPF and his project is creating a managed assembly dll. // 2. The user opens VS and creates a project and builds it. // 3. Then the user opens VS creates another project and adds a reference to the previously built assembly. This will load the assembly in the appdomain had we been using Assembly.ReflectionOnlyLoadFrom. // 4. Then he goes back to the first project modifies it an builds it. A build error is issued that the assembly is used. // GetAssemblyName is assured not to load the assembly. tryToCreateAnAssemblyReference = (AssemblyName.GetAssemblyName(selectorData.bstrFile) != null); } catch (BadImageFormatException) { // We have found the file and it is not a .NET assembly; no need to try to // load it again. tryToCreateAnAssemblyReference = false; } catch (FileLoadException) { // We must still try to load from here because this exception is thrown if we want // to add the same assembly refererence from different locations. tryToCreateAnAssemblyReference = true; } } TEntryPointFileNode node = null; if (tryToCreateAnAssemblyReference) { // This might be a candidate for an assembly reference node. Try to load it. // CreateAssemblyReferenceNode will suppress BadImageFormatException if the node cannot be created. node = this.CreateAssemblyReferenceNode(selectorData.bstrFile); } // If no node has been created try to create a com reference node. if (node == null) { if (!File.Exists(selectorData.bstrFile)) { return(null); } node = this.CreateComReferenceNode(selectorData); } return(node); }
public IList <TEntryPointFileNode> EnumReferences() { List <TEntryPointFileNode> refs = new List <TEntryPointFileNode>(); for (HierarchyNode node = this.FirstChild; node != null; node = node.NextSibling) { TEntryPointFileNode refNode = node as TEntryPointFileNode; if (refNode != null) { refs.Add(refNode); } } return(refs); }
protected virtual TEntryPointFileNode CreateReferenceNode(string referenceType, ProjectElement element) { TEntryPointFileNode node = null; if (referenceType == ProjectFileConstants.COMReference) { node = this.CreateComReferenceNode(element); } else if (referenceType == ProjectFileConstants.Reference) { node = this.CreateAssemblyReferenceNode(element); } else if (referenceType == ProjectFileConstants.ProjectReference) { node = this.CreateProjectReferenceNode(element); } return(node); }
/// <summary> /// Adds references to this container from a MSBuild project. /// </summary> public void LoadReferencesFromBuildProject(MSBuild.Project buildProject) { foreach (string referenceType in SupportedReferenceTypes) { MSBuild.BuildItemGroup refererncesGroup = buildProject.GetEvaluatedItemsByName(referenceType); bool isAssemblyReference = referenceType == ProjectFileConstants.Reference; if (isAssemblyReference && this.ProjectMgr.Build(MsBuildTarget.ResolveAssemblyReferences) != MSBuildResult.Sucessful) { continue; } foreach (MSBuild.BuildItem item in refererncesGroup) { ProjectElement element = new ProjectElement(this.ProjectMgr, item, false); TEntryPointFileNode node = CreateReferenceNode(referenceType, element); if (node != null) { // Make sure that we do not want to add the item twice to the ui hierarchy // We are using here the UI representation of the Node namely the Caption to find that out, in order to // avoid different representation problems. // Example :<Reference Include="EnvDTE80, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> // <Reference Include="EnvDTE80" /> bool found = false; for (HierarchyNode n = this.FirstChild; n != null && !found; n = n.NextSibling) { if (String.Compare(n.Caption, node.Caption, StringComparison.OrdinalIgnoreCase) == 0) { found = true; } } if (!found) { this.AddChild(node); } } } } }
/// <summary> /// Checks if a reference is already added. The method parses all references and compares the Url. /// </summary> /// <returns>true if the assembly has already been added.</returns> protected virtual bool IsAlreadyAdded() { ReferenceContainerNode referencesFolder = this.ProjectMgr.FindChild(ReferenceContainerNode.ReferencesNodeVirtualName) as ReferenceContainerNode; Debug.Assert(referencesFolder != null, "Could not find the References node"); for (HierarchyNode n = referencesFolder.FirstChild; n != null; n = n.NextSibling) { TEntryPointFileNode refererenceNode = n as TEntryPointFileNode; if (null != refererenceNode) { // We check if the Url of the assemblies is the same. if (NativeMethods.IsSamePath(refererenceNode.Url, this.Url)) { return(true); } } } return(false); }
protected virtual TEntryPointFileNode CreateReferenceNode(VSCOMPONENTSELECTORDATA selectorData) { TEntryPointFileNode node = null; switch (selectorData.type) { case VSCOMPONENTTYPE.VSCOMPONENTTYPE_Project: node = this.CreateProjectReferenceNode(selectorData); break; case VSCOMPONENTTYPE.VSCOMPONENTTYPE_File: // This is the case for managed assembly case VSCOMPONENTTYPE.VSCOMPONENTTYPE_ComPlus: node = this.CreateFileComponent(selectorData); break; case VSCOMPONENTTYPE.VSCOMPONENTTYPE_Com2: node = this.CreateComReferenceNode(selectorData); break; } return(node); }
/// <summary> /// Adds a reference to this container using the selector data structure to identify it. /// </summary> public TEntryPointFileNode AddReferenceFromSelectorData(VSCOMPONENTSELECTORDATA selectorData) { if (!this.ProjectMgr.QueryEditProjectFile(false)) { throw Marshal.GetExceptionForHR(VSConstants.OLE_E_PROMPTSAVECANCELLED); } TEntryPointFileNode node = CreateReferenceNode(selectorData); if (node != null) { // This call will find if the reference is in the project and, in this case // will not add it again, so the parent node will not be set. node.AddReference(); if (null == node.Parent) { // The reference was not added, so we can not return this item because it // is not inside the project. return(null); } } return(node); }