protected override void ApplyMetadataReferenceRemoved(ProjectId projectId, MetadataReference metadataReference)
        {
            if (projectId == null)
            {
                throw new ArgumentNullException("projectId");
            }

            if (metadataReference == null)
            {
                throw new ArgumentNullException("metadataReference");
            }

            IVisualStudioHostProject hostProject;
            IVsHierarchy             hierarchy;

            EnvDTE.Project project;
            GetProjectData(projectId, out hostProject, out hierarchy, out project);

            string filePath = GetMetadataPath(metadataReference);

            if (filePath != null)
            {
                VSLangProj.VSProject vsProject = (VSLangProj.VSProject)project.Object;
                VSLangProj.Reference reference = vsProject.References.Find(filePath);
                if (reference != null)
                {
                    reference.Remove();
                }
            }
        }
Example #2
0
        /// <summary>
        /// Adds (or verifies we already have) all the given assembly references in the active project
        /// </summary>
        /// <param name="references">Set of references to add to active project</param>
        private void AddReferences(IEnumerable <string> references)
        {
            if (references.Any())
            {
                // Get active project -- throws if not available
                Project project = this.ActiveProject;
                VSLangProj.VSProject vsProject = project.Object as VSLangProj.VSProject;
                if (vsProject != null)
                {
                    // Unconditionally add every reference.
                    // The References.Add method contains the logic to check for an existing
                    // reference of the same identity, and if found, will not change anything.
                    foreach (string assemblyReference in references)
                    {
                        VSLangProj.Reference reference = vsProject.References.Add(assemblyReference);

                        // The assembly reference string can have the full assembly identity format ('AssemblyName, Version=xxxx, Culture ...')
                        // or it can be the full assembly file path.
                        // If the assembly reference is a full path, assume it is not in the GAC and copy it locally to the project
                        // so it can find it and also for bin-deployment support.
                        if (ShouldCopyLocal(assemblyReference))
                        {
                            reference.CopyLocal = true;
                        }
                    }
                }
            }
        }
Example #3
0
 public static string GetFullName(VSLangProj.Reference reference)
 {
     return(string.Format("{ 0}, Version ={ 1}.{ 2}.{ 3}.{ 4}, Culture ={ 5}, PublicKeyToken ={ 6}",
                          reference.Name,
                          reference.MajorVersion, reference.MinorVersion, reference.BuildNumber, reference.RevisionNumber,
                          reference.Culture.Or("neutral"),
                          reference.PublicKeyToken.Or("null")));
 }
Example #4
0
        private void AddReference(Project projectNeedingReference, Project projectRemoving, string filePath)
        {
            var vsProject = ((VSProject)projectNeedingReference.Object);

            filePath = ToAbsolute(filePath, vsProject.Project.FullName);

            foreach (Reference reference in vsProject.References)
            {
                if (reference.SourceProject == null)
                {
                    continue;
                }

                if (reference.SourceProject.FullName == projectRemoving.FullName)
                {
                    reference.Remove();
                    break;
                }
            }

            VSLangProj.Reference newRef = vsProject.References.Add(filePath);

            if (!newRef.Path.Equals(filePath, StringComparison.OrdinalIgnoreCase))
            {
                Microsoft.Build.Evaluation.Project     msBuildProj = Microsoft.Build.Evaluation.ProjectCollection.GlobalProjectCollection.GetLoadedProjects(projectNeedingReference.FullName).First();
                Microsoft.Build.Evaluation.ProjectItem msBuildRef  = null;

                AssemblyName newFileAssemblyName = AssemblyName.GetAssemblyName(filePath);
                foreach (var item in msBuildProj.GetItems("Reference"))
                {
                    AssemblyName refAssemblyName = null;
                    try
                    {
                        refAssemblyName = new AssemblyName(item.EvaluatedInclude);
                    }
                    catch { }

                    if (refAssemblyName != null)
                    {
                        var refToken = refAssemblyName.GetPublicKeyToken() ?? new byte[0];
                        var newToken = newFileAssemblyName.GetPublicKeyToken() ?? new byte[0];

                        if
                        (
                            refAssemblyName.Name.Equals(newFileAssemblyName.Name, StringComparison.OrdinalIgnoreCase) &&
                            ((refAssemblyName.Version != null && refAssemblyName.Version.Equals(newFileAssemblyName.Version)) ||
                             (refAssemblyName.Version == null && newFileAssemblyName.Version == null)) &&
                            (refAssemblyName.CultureInfo != null && (refAssemblyName.CultureInfo.Equals(newFileAssemblyName.CultureInfo)) ||
                             (refAssemblyName.CultureInfo == null && newFileAssemblyName.CultureInfo == null)) &&
                            ((Enumerable.SequenceEqual(refToken, newToken)))
                        )
                        {
                            msBuildRef = item;
                            break;
                        }
                    }
                }

                if (msBuildRef != null)
                {
                    _ = msBuildRef.SetMetadataValue("HintPath", ToRelative(filePath, projectNeedingReference.FullName));
                }
            }
        }