internal static AssemblyNameExtension GetHighestVersionInRedist(InstalledAssemblies installedAssemblies, AssemblyNameExtension assemblyName)
 {
     AssemblyNameExtension extension = assemblyName;
     if ((extension.Version == null) && (installedAssemblies != null))
     {
         AssemblyEntry[] entryArray = installedAssemblies.FindAssemblyNameFromSimpleName(assemblyName.Name);
         if (entryArray.Length <= 1)
         {
             return extension;
         }
         for (int i = 0; i < entryArray.Length; i++)
         {
             AssemblyNameExtension that = new AssemblyNameExtension(entryArray[i].FullName);
             if (((that.Version != null) && (that.Version.CompareTo(extension.Version) > 0)) && assemblyName.PartialNameCompare(that, PartialComparisonFlags.PublicKeyToken | PartialComparisonFlags.Culture))
             {
                 extension = that;
             }
         }
     }
     return extension;
 }
 protected bool FileMatchesAssemblyName(AssemblyNameExtension assemblyName, bool isPrimaryProjectReference, bool wantSpecificVersion, bool allowMismatchBetweenFusionNameAndFileName, string pathToCandidateAssembly, ResolutionSearchLocation searchLocation)
 {
     searchLocation.FileNameAttempted = pathToCandidateAssembly;
     if (!allowMismatchBetweenFusionNameAndFileName)
     {
         string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(pathToCandidateAssembly);
         if (string.Compare(assemblyName.Name, fileNameWithoutExtension, StringComparison.CurrentCultureIgnoreCase) != 0)
         {
             if (searchLocation != null)
             {
                 if (fileNameWithoutExtension.Length > 0)
                 {
                     searchLocation.AssemblyName = new AssemblyNameExtension(fileNameWithoutExtension);
                     searchLocation.Reason = NoMatchReason.FusionNamesDidNotMatch;
                 }
                 else
                 {
                     searchLocation.Reason = NoMatchReason.TargetHadNoFusionName;
                 }
             }
             return false;
         }
     }
     bool flag = (assemblyName != null) && assemblyName.IsSimpleName;
     if (this.fileExists(pathToCandidateAssembly))
     {
         if (!this.compareProcessorArchitecture)
         {
             if (((assemblyName == null) && isPrimaryProjectReference) && !wantSpecificVersion)
             {
                 return true;
             }
             if ((isPrimaryProjectReference && !wantSpecificVersion) && flag)
             {
                 return true;
             }
         }
         AssemblyNameExtension that = null;
         try
         {
             that = this.getAssemblyName(pathToCandidateAssembly);
         }
         catch (FileLoadException)
         {
         }
         if (searchLocation != null)
         {
             searchLocation.AssemblyName = that;
         }
         if (that != null)
         {
             if (((this.compareProcessorArchitecture && (that.AssemblyName.ProcessorArchitecture != this.targetProcessorArchitecture)) && ((this.targetProcessorArchitecture != ProcessorArchitecture.None) && (that.AssemblyName.ProcessorArchitecture != ProcessorArchitecture.None))) && ((this.targetProcessorArchitecture != ProcessorArchitecture.MSIL) && (that.AssemblyName.ProcessorArchitecture != ProcessorArchitecture.MSIL)))
             {
                 searchLocation.Reason = NoMatchReason.ProcessorArchitectureDoesNotMatch;
                 return false;
             }
             bool flag2 = (wantSpecificVersion && (assemblyName != null)) && assemblyName.Equals(that);
             bool flag3 = (!wantSpecificVersion && (assemblyName != null)) && assemblyName.PartialNameCompare(that);
             if (flag2 || flag3)
             {
                 return true;
             }
             if (searchLocation != null)
             {
                 searchLocation.Reason = NoMatchReason.FusionNamesDidNotMatch;
             }
         }
         else if (searchLocation != null)
         {
             searchLocation.Reason = NoMatchReason.TargetHadNoFusionName;
         }
     }
     else if (searchLocation != null)
     {
         searchLocation.Reason = NoMatchReason.FileNotFound;
     }
     return false;
 }
        public void TestAssemblyPatialMatchSimpleNamePublicKeyToken()
        {
            AssemblyNameExtension assemblyNameToMatchPublicToken = new AssemblyNameExtension("System.Xml, PublicKeyToken=b03f5f7f11d50a3a");
            AssemblyNameExtension assemblyNameToNotMatch = new AssemblyNameExtension("System.Xml, PublicKeyToken=b03f5f7f11d50a3b");
            AssemblyNameExtension assemblyMatchNoVersion = new AssemblyNameExtension("System.Xml");

            foreach (string assembly in assembliesForPartialMatch)
            {
                AssemblyNameExtension assemblyToCompare = new AssemblyNameExtension(assembly);

                // If there is a version make sure the assembly name with the correct publicKeyToken matches
                // Make sure the assembly with the wrong publicKeyToken does not match
                if (assemblyToCompare.GetPublicKeyToken() != null)
                {
                    Assert.IsTrue(assemblyNameToMatchPublicToken.PartialNameCompare(assemblyToCompare));
                    Assert.IsTrue(assemblyNameToMatchPublicToken.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.PublicKeyToken));

                    Assert.IsFalse(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare));
                    Assert.IsFalse(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.PublicKeyToken));

                    // Matches because publicKeyToken is not specified
                    Assert.IsTrue(assemblyMatchNoVersion.PartialNameCompare(assemblyToCompare));
                    Assert.IsTrue(assemblyMatchNoVersion.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.PublicKeyToken));
                }
                else
                {
                    // If there is no version make names with a publicKeyToken specified do not match
                    Assert.IsFalse(assemblyNameToMatchPublicToken.PartialNameCompare(assemblyToCompare));
                    Assert.IsFalse(assemblyNameToMatchPublicToken.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.PublicKeyToken));

                    Assert.IsFalse(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare));
                    Assert.IsFalse(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.PublicKeyToken));

                    // Matches because publicKeyToken is not specified
                    Assert.IsTrue(assemblyMatchNoVersion.PartialNameCompare(assemblyToCompare));
                    Assert.IsTrue(assemblyMatchNoVersion.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.PublicKeyToken));
                }
            }
        }
        public void TestAssemblyPatialMatchSimpleNameCulture()
        {
            AssemblyNameExtension assemblyNameToMatchCulture = new AssemblyNameExtension("System.Xml, Culture=en");
            AssemblyNameExtension assemblyNameToNotMatch = new AssemblyNameExtension("System.Xml, Culture=de-DE");
            AssemblyNameExtension assemblyMatchNoVersion = new AssemblyNameExtension("System.Xml");

            foreach (string assembly in assembliesForPartialMatch)
            {
                AssemblyNameExtension assemblyToCompare = new AssemblyNameExtension(assembly);

                // If there is a version make sure the assembly name with the correct culture matches
                // Make sure the assembly with the wrong culture does not match
                if (assemblyToCompare.CultureInfo != null)
                {
                    Assert.IsTrue(assemblyNameToMatchCulture.PartialNameCompare(assemblyToCompare));
                    Assert.IsTrue(assemblyNameToMatchCulture.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.Culture));

                    Assert.IsFalse(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare));
                    Assert.IsFalse(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.Culture));

                    // Matches because culture is not specified
                    Assert.IsTrue(assemblyMatchNoVersion.PartialNameCompare(assemblyToCompare));
                    Assert.IsTrue(assemblyMatchNoVersion.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.Culture));
                }
                else
                {
                    // If there is no version make names with a culture specified do not match
                    Assert.IsFalse(assemblyNameToMatchCulture.PartialNameCompare(assemblyToCompare));
                    Assert.IsFalse(assemblyNameToMatchCulture.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.Culture));

                    Assert.IsFalse(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare));
                    Assert.IsFalse(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.Culture));

                    // Matches because culture is not specified
                    Assert.IsTrue(assemblyMatchNoVersion.PartialNameCompare(assemblyToCompare));
                    Assert.IsTrue(assemblyMatchNoVersion.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.Culture));
                }
            }
        }
        public void TestAssemblyPatialMatchSimpleNameVersion()
        {
            AssemblyNameExtension assemblyNameToMatchVersion = new AssemblyNameExtension("System.Xml, Version=10.0.0.0");
            AssemblyNameExtension assemblyNameToNotMatch = new AssemblyNameExtension("System.Xml, Version=5.0.0.0");
            AssemblyNameExtension assemblyMatchNoVersion = new AssemblyNameExtension("System.Xml");

            foreach (string assembly in assembliesForPartialMatch)
            {
                AssemblyNameExtension assemblyToCompare = new AssemblyNameExtension(assembly);

                // If there is a version make sure the assembly name with the correct version matches
                // Make sure the assembly with the wrong version does not match
                if (assemblyToCompare.Version != null)
                {
                    Assert.IsTrue(assemblyNameToMatchVersion.PartialNameCompare(assemblyToCompare));
                    Assert.IsTrue(assemblyNameToMatchVersion.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.Version));

                    Assert.IsFalse(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare));
                    Assert.IsFalse(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.Version));

                    // Matches because version is not specified
                    Assert.IsTrue(assemblyMatchNoVersion.PartialNameCompare(assemblyToCompare));
                    Assert.IsTrue(assemblyMatchNoVersion.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.Version));
                }
                else
                {

                    // If there is no version make names with a version specified do not match
                    Assert.IsFalse(assemblyNameToMatchVersion.PartialNameCompare(assemblyToCompare));
                    Assert.IsFalse(assemblyNameToMatchVersion.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.Version));

                    Assert.IsFalse(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare));
                    Assert.IsFalse(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.Version));

                    // Matches because version is not specified
                    Assert.IsTrue(assemblyMatchNoVersion.PartialNameCompare(assemblyToCompare));
                    Assert.IsTrue(assemblyMatchNoVersion.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName | PartialComparisonFlags.Version));
                }
            }
        }
        public void TestAssemblyPatialMatchSimpleName()
        {
            AssemblyNameExtension assemblyNameToMatch = new AssemblyNameExtension("System.Xml");
            AssemblyNameExtension assemblyNameToNotMatch = new AssemblyNameExtension("System.Xmla");

            foreach (string assembly in assembliesForPartialMatch)
            {
                AssemblyNameExtension assemblyToCompare = new AssemblyNameExtension(assembly);

                Assert.IsTrue(assemblyNameToMatch.PartialNameCompare(assemblyToCompare));
                Assert.IsTrue(assemblyNameToMatch.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName));
                Assert.IsFalse(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare));
                Assert.IsFalse(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName));
            }
        }
 public void CreateAssemblyNameExtensionWithNoSimpleName2()
 {
     AssemblyNameExtension extension = new AssemblyNameExtension("Version=2.0.0.0, Culture=en, PublicKeyToken=b03f5f7f11d50a3a");
     AssemblyNameExtension extension2 = new AssemblyNameExtension("A, Version=2.0.0.0, Culture=en, PublicKeyToken=b03f5f7f11d50a3a");
     extension2.PartialNameCompare(extension);
 }
        public void TestAssemblyPartialMatchSimpleNameRetargetable()
        {
            AssemblyNameExtension assemblyNameToMatchRetargetable = new AssemblyNameExtension("System.Xml, Version=10.0.0.0, Culture=en, PublicKeyToken=b03f5f7f11d50a3a, Retargetable=Yes");
            AssemblyNameExtension assemblyNameToNotMatch = new AssemblyNameExtension("System.Xml, Version=10.0.0.0, Culture=en, PublicKeyToken=b03f5f7f11d50a3a, Retargetable=No");
            AssemblyNameExtension assemblyMatchNoRetargetable = new AssemblyNameExtension("System.Xml");

            foreach (string assembly in s_assembliesForPartialMatch)
            {
                AssemblyNameExtension assemblyToCompare = new AssemblyNameExtension(assembly);

                if (assemblyToCompare.FullName.IndexOf("Retargetable=Yes", StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    Assert.IsTrue(assemblyNameToMatchRetargetable.PartialNameCompare(assemblyToCompare));
                    Assert.IsTrue(assemblyNameToMatchRetargetable.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName, true));

                    Assert.IsTrue(assemblyToCompare.PartialNameCompare(assemblyNameToNotMatch));
                    Assert.IsFalse(assemblyToCompare.PartialNameCompare(assemblyNameToNotMatch, PartialComparisonFlags.SimpleName, true));

                    Assert.IsFalse(assemblyToCompare.PartialNameCompare(assemblyMatchNoRetargetable));
                    Assert.IsFalse(assemblyToCompare.PartialNameCompare(assemblyMatchNoRetargetable, PartialComparisonFlags.SimpleName, true));

                    Assert.IsTrue(assemblyMatchNoRetargetable.PartialNameCompare(assemblyToCompare));
                    Assert.IsFalse(assemblyMatchNoRetargetable.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName, true));
                }
                else
                {
                    Assert.IsFalse(assemblyNameToMatchRetargetable.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName, true));

                    // Match because retargetable false is the same as no retargetable bit
                    bool match = assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare);
                    if (assemblyToCompare.FullName.IndexOf("System.Xml, Version=10.0.0.0, Culture=en, PublicKeyToken=b03f5f7f11d50a3a", StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        Assert.IsTrue(match);
                    }
                    else
                    {
                        Assert.IsFalse(match);
                    }
                    Assert.IsTrue(assemblyNameToNotMatch.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName, true));

                    Assert.IsTrue(assemblyMatchNoRetargetable.PartialNameCompare(assemblyToCompare));
                    Assert.IsTrue(assemblyMatchNoRetargetable.PartialNameCompare(assemblyToCompare, PartialComparisonFlags.SimpleName, true));
                }
            }
        }
        /// <summary>
        /// If the version is not set for an assembly reference, go through the redist list and find the highest version for that assembly.
        /// Make sure when matching the assembly in the redist that we take into account the publicKeyToken and the Culture.
        /// </summary>
        internal static AssemblyNameExtension GetHighestVersionInRedist(InstalledAssemblies installedAssemblies, AssemblyNameExtension assemblyName)
        {
            AssemblyNameExtension assemblyNameToUse = assemblyName;

            if ((assemblyNameToUse.Version == null && installedAssemblies != null))
            {
                // If there are multiple entries in the redist list for this assembly, let's
                // pick the one with the highest version and resolve it.

                AssemblyEntry[] assemblyEntries = installedAssemblies.FindAssemblyNameFromSimpleName(assemblyName.Name);

                if (assemblyEntries.Length != 0)
                {
                    for (int i = 0; i < assemblyEntries.Length; ++i)
                    {
                        AssemblyNameExtension current = new AssemblyNameExtension(assemblyEntries[i].FullName);

                        // If the current version is higher than the previously looked at.
                        if (current.Version != null && current.Version.CompareTo(assemblyNameToUse.Version) > 0)
                        {
                            // Only compare the Culture and the public key token, the simple names will ALWAYS be the same and the version we do not care about.
                            if (assemblyName.PartialNameCompare(current, PartialComparisonFlags.Culture | PartialComparisonFlags.PublicKeyToken))
                            {
                                assemblyNameToUse = current;
                            }
                        }
                    }
                }
            }

            return assemblyNameToUse;
        }
示例#10
0
        /// <summary>
        /// Determines whether an assembly name matches the assembly pointed to by pathToCandidateAssembly
        /// </summary>
        /// <param name="assemblyName">The assembly name to look up.</param>
        /// <param name="isPrimaryProjectReference">True if this is a primary reference directly from the project file.</param>
        /// <param name="wantSpecificVersion">Whether the version needs to match exactly or loosely.</param>
        /// <param name="pathToCandidateAssembly">Path to a possible file.</param>
        /// <param name="searchLocation">Information about why the candidate file didn't match</param>
        /// <param name="fileExists">Delegate for File.Exists.</param>
        /// <param name="getAssemblyName">Delegate for AssemblyName.GetAssemblyName</param>
        /// <returns></returns>
        protected bool FileMatchesAssemblyName
        (
            AssemblyNameExtension assemblyName,
            bool isPrimaryProjectReference,
            bool wantSpecificVersion,
            bool allowMismatchBetweenFusionNameAndFileName,
            string pathToCandidateAssembly,
            ResolutionSearchLocation searchLocation
        )
        {
            searchLocation.FileNameAttempted = pathToCandidateAssembly;

            // Base name of the target file has to match the Name from the assemblyName
            if (!allowMismatchBetweenFusionNameAndFileName)
            {
                string candidateBaseName = Path.GetFileNameWithoutExtension(pathToCandidateAssembly);
                if (String.Compare(assemblyName.Name, candidateBaseName, StringComparison.CurrentCultureIgnoreCase) != 0)
                {
                    if (searchLocation != null)
                    {
                        if (candidateBaseName.Length > 0)
                        {
                            searchLocation.AssemblyName = new AssemblyNameExtension(candidateBaseName);
                            searchLocation.Reason = NoMatchReason.FusionNamesDidNotMatch;
                        }
                        else
                        {
                            searchLocation.Reason = NoMatchReason.TargetHadNoFusionName;
                        }
                    }
                    return false;
                }
            }

            bool isSimpleAssemblyName = assemblyName == null ? false : assemblyName.IsSimpleName;

            if (fileExists(pathToCandidateAssembly))
            {
                // If the resolver we are using is targeting a given processor architecture then we must crack open the assembly and make sure the architecture is compatible
                // We cannot do these simple name matches.
                if (!compareProcessorArchitecture)
                {
                    // If the file existed and the reference is a simple primary reference which does not contain an assembly name (say a raw file name)
                    // then consider this a match.
                    if (assemblyName == null && isPrimaryProjectReference && !wantSpecificVersion)
                    {
                        return true;
                    }

                    if (isPrimaryProjectReference && !wantSpecificVersion && isSimpleAssemblyName)
                    {
                        return true;
                    }
                }

                // We have strong name information, so do some added verification here.
                AssemblyNameExtension targetAssemblyName = null;
                try
                {
                    targetAssemblyName = getAssemblyName(pathToCandidateAssembly);
                }
                catch (System.IO.FileLoadException)
                {
                    // Its pretty hard to get here, you need an assembly that contains a valid reference
                    // to a dependent assembly that, in turn, throws a FileLoadException during GetAssemblyName.
                    // Still it happened once, with an older version of the CLR. 

                    // ...falling through and relying on the targetAssemblyName==null behavior below...
                }

                if (searchLocation != null)
                {
                    searchLocation.AssemblyName = targetAssemblyName;
                }

                // targetAssemblyName may be null if there was no metadata for this assembly.
                // In this case, there's no match.
                if (targetAssemblyName != null)
                {
                    // If we are targeting a given processor architecture check to see if they match, if we are targeting MSIL then any architecture will do.
                    if (compareProcessorArchitecture)
                    {
                        // Only reject the assembly if the target processor architecture does not match the assemby processor architecture and the assembly processor architecture is not NONE or MSIL.
                        if (
                              targetAssemblyName.AssemblyName.ProcessorArchitecture != targetProcessorArchitecture &&  /* The target and assembly architectures do not match*/
                              (targetProcessorArchitecture != ProcessorArchitecture.None && targetAssemblyName.AssemblyName.ProcessorArchitecture != ProcessorArchitecture.None)  /*The assembly is not none*/
                              && (targetProcessorArchitecture != ProcessorArchitecture.MSIL && targetAssemblyName.AssemblyName.ProcessorArchitecture != ProcessorArchitecture.MSIL) /*The assembly is not MSIL*/
                           )
                        {
                            searchLocation.Reason = NoMatchReason.ProcessorArchitectureDoesNotMatch;
                            return false;
                        }
                    }

                    bool matchedSpecificVersion = (wantSpecificVersion && assemblyName != null && assemblyName.Equals(targetAssemblyName));
                    bool matchPartialName = !wantSpecificVersion && assemblyName != null && assemblyName.PartialNameCompare(targetAssemblyName);

                    if (matchedSpecificVersion || matchPartialName)
                    {
                        return true;
                    }
                    else
                    {
                        // Reason was: FusionNames did not match.
                        if (searchLocation != null)
                        {
                            searchLocation.Reason = NoMatchReason.FusionNamesDidNotMatch;
                        }
                    }
                }
                else
                {
                    // Reason was: Target had no fusion name.
                    if (searchLocation != null)
                    {
                        searchLocation.Reason = NoMatchReason.TargetHadNoFusionName;
                    }
                }
            }
            else
            {
                // Reason was: No file found at that location.
                if (searchLocation != null)
                {
                    searchLocation.Reason = NoMatchReason.FileNotFound;
                }
            }

            return false;
        }