Esempio n. 1
0
        /// <summary>
        /// Looks up specified partial assembly name in the GAC and returns the best matching full assembly name/>.
        /// </summary>
        /// <param name="displayName">The display name of an assembly.</param>
        /// <param name="location">Full path name of the resolved assembly.</param>
        /// <param name="architectureFilter">The optional processor architecture.</param>
        /// <param name="preferredCulture">The optional preferred culture information.</param>
        /// <returns>An assembly identity or null, if <paramref name="displayName"/> can't be resolved.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="displayName"/> is null.</exception>
        public static unsafe string ResolvePartialName(
            string displayName,
            out string location,
            ProcessorArchitecture[] architectureFilter = null,
            CultureInfo preferredCulture = null)
        {
            if (displayName == null)
            {
                throw new ArgumentNullException("displayName");
            }

            location = null;
            FusionAssemblyIdentity.IAssemblyName nameObject = FusionAssemblyIdentity.ToAssemblyNameObject(displayName);
            if (nameObject == null)
            {
                return(null);
            }

            var    candidates  = GetAssemblyObjects(nameObject, architectureFilter);
            string cultureName = (preferredCulture != null && !preferredCulture.IsNeutralCulture) ? preferredCulture.Name : null;

            var bestMatch = FusionAssemblyIdentity.GetBestMatch(candidates, cultureName);

            if (bestMatch == null)
            {
                return(null);
            }

            string fullName = FusionAssemblyIdentity.GetDisplayName(bestMatch, FusionAssemblyIdentity.ASM_DISPLAYF.FULL);

            fixed(char *p = new char[MAX_PATH])
            {
                ASSEMBLY_INFO info = new ASSEMBLY_INFO
                {
                    cbAssemblyInfo            = (uint)Marshal.SizeOf(typeof(ASSEMBLY_INFO)),
                    pszCurrentAssemblyPathBuf = p,
                    cchBuf = (uint)MAX_PATH
                };

                IAssemblyCache assemblyCacheObject;

                CreateAssemblyCache(out assemblyCacheObject, 0);
                assemblyCacheObject.QueryAssemblyInfo(0, fullName, ref info);
                Debug.Assert(info.pszCurrentAssemblyPathBuf != null);
                Debug.Assert(info.pszCurrentAssemblyPathBuf[info.cchBuf - 1] == '\0');

                var result = Marshal.PtrToStringUni((IntPtr)info.pszCurrentAssemblyPathBuf, (int)info.cchBuf - 1);

                Debug.Assert(result.IndexOf('\0') == -1);
                location = result;
            }

            return(fullName);
        }
Esempio n. 2
0
        public void FusionGetBestMatch()
        {
            var foo       = FusionAssemblyIdentity.ToAssemblyNameObject("foo");
            var foo1      = FusionAssemblyIdentity.ToAssemblyNameObject("foo, Version=1.0.0.0, Culture=neutral");
            var foo2      = FusionAssemblyIdentity.ToAssemblyNameObject("foo, Version=2.0.0.0, Culture=neutral");
            var foo3      = FusionAssemblyIdentity.ToAssemblyNameObject("foo, Version=3.0.0.0, Culture=neutral");
            var foo3_enUS = FusionAssemblyIdentity.ToAssemblyNameObject("foo, Version=3.0.0.0, Culture=en-US");
            var foo3_deDE = FusionAssemblyIdentity.ToAssemblyNameObject("foo, Version=3.0.0.0, Culture=de-DE");

            var m = FusionAssemblyIdentity.GetBestMatch(new[] { foo2, foo1, foo3 }, null);

            Assert.Equal(foo3, m);

            m = FusionAssemblyIdentity.GetBestMatch(new[] { foo3, foo2, foo1 }, null);
            Assert.Equal(foo3, m);

            // only simple name is used
            m = FusionAssemblyIdentity.GetBestMatch(new[] { foo2, foo3 }, null);
            Assert.Equal(foo3, m);

            // the first match if preferred cultures not specified
            m = FusionAssemblyIdentity.GetBestMatch(new[] { foo1, foo3_deDE, foo3_enUS, foo2 }, null);
            Assert.Equal(foo3_deDE, m);

            // the first match if preferred cultures not specified
            m = FusionAssemblyIdentity.GetBestMatch(new[] { foo1, foo3_deDE, foo3_enUS, foo2 }, null);
            Assert.Equal(foo3_deDE, m);

            m = FusionAssemblyIdentity.GetBestMatch(new[] { foo1, foo3, foo3_deDE, foo3_enUS, foo2 }, "en-US");
            Assert.Equal(foo3_enUS, m);

            m = FusionAssemblyIdentity.GetBestMatch(new[] { foo1, foo3_deDE, foo3, foo3_enUS, foo2 }, "cz-CZ");
            Assert.Equal(foo3, m);

            m = FusionAssemblyIdentity.GetBestMatch(new[] { foo3_deDE, foo2 }, "en-US");
            Assert.Equal(foo3_deDE, m);

            // neutral culture wins over specific non-matching one:
            m = FusionAssemblyIdentity.GetBestMatch(new[] { foo3_deDE, foo3, foo2 }, "en-US");
            Assert.Equal(foo3, m);
        }