Esempio n. 1
0
        /// <summary>
        /// Enumerates assemblies in the GAC returning those that match given partial name and
        /// architecture.
        /// </summary>
        /// <param name="partialName">The optional partial name.</param>
        /// <param name="architectureFilter">The optional architecture filter.</param>
        public static IEnumerable <AssemblyIdentity> GetAssemblyIdentities(string partialName = null, Func <ProcessorArchitecture, bool> architectureFilter = null)
        {
            FusionAssemblyIdentity.IAssemblyName nameObj;
            if (partialName != null)
            {
                nameObj = FusionAssemblyIdentity.ToAssemblyNameObject(partialName);
                if (nameObj == null)
                {
                    return(Enumerable.Empty <AssemblyIdentity>());
                }
            }
            else
            {
                nameObj = null;
            }

            return(GetAssemblyIdentities(nameObj, architectureFilter));
        }
Esempio n. 2
0
        /// <summary>
        /// Enumerates assemblies in the GAC returning those that match given partial name and
        /// architecture.
        /// </summary>
        /// <param name="partialName">The optional partial name.</param>
        /// <param name="architectureFilter">The optional architecture filter.</param>
        public override IEnumerable <AssemblyIdentity> GetAssemblyIdentities(string partialName = null, ImmutableArray <ProcessorArchitecture> architectureFilter = default(ImmutableArray <ProcessorArchitecture>))
        {
            FusionAssemblyIdentity.IAssemblyName nameObj;
            if (partialName != null)
            {
                nameObj = FusionAssemblyIdentity.ToAssemblyNameObject(partialName);
                if (nameObj == null)
                {
                    return(SpecializedCollections.EmptyEnumerable <AssemblyIdentity>());
                }
            }
            else
            {
                nameObj = null;
            }

            return(GetAssemblyIdentities(nameObj, architectureFilter));
        }
        private static AssemblyIdentity ResolvePartialName(
            string displayName,
            Func <ProcessorArchitecture, bool> architectureFilter,
            CultureInfo preferredCulture,
            out string location,
            bool resolveLocation)
        {
            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);
            }

            if (resolveLocation)
            {
                location = GetAssemblyLocation(bestMatch);
            }

            return(null);
            //return FusionAssemblyIdentity.ToAssemblyIdentity(bestMatch);
        }
Esempio n. 4
0
        public override AssemblyIdentity ResolvePartialName(
            string displayName,
            out string location,
            ImmutableArray <ProcessorArchitecture> architectureFilter,
            CultureInfo preferredCulture
            )
        {
            if (displayName == null)
            {
                throw new ArgumentNullException(nameof(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);
            }

            location = GetAssemblyLocation(bestMatch);
            return(FusionAssemblyIdentity.ToAssemblyIdentity(bestMatch));
        }
 private static extern int CreateAssemblyEnum(out IAssemblyEnum ppEnum, FusionAssemblyIdentity.IApplicationContext pAppCtx, FusionAssemblyIdentity.IAssemblyName pName, ASM_CACHE dwFlags, IntPtr pvReserved);
        internal static unsafe string GetAssemblyLocation(FusionAssemblyIdentity.IAssemblyName nameObject)
        {
            // NAME | VERSION | CULTURE | PUBLIC_KEY_TOKEN | RETARGET | PROCESSORARCHITECTURE
            string fullName = FusionAssemblyIdentity.GetDisplayName(nameObject, 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);
                return result;
            }
        }
        // Internal for testing.
        internal static IEnumerable<FusionAssemblyIdentity.IAssemblyName> GetAssemblyObjects(
            FusionAssemblyIdentity.IAssemblyName partialNameFilter,
            ImmutableArray<ProcessorArchitecture> architectureFilter)
        {
            IAssemblyEnum enumerator;
            FusionAssemblyIdentity.IApplicationContext applicationContext = null;

            int hr = CreateAssemblyEnum(out enumerator, applicationContext, partialNameFilter, ASM_CACHE.GAC, IntPtr.Zero);
            if (hr == S_FALSE)
            {
                // no assembly found
                yield break;
            }
            else if (hr != S_OK)
            {
                Exception e = Marshal.GetExceptionForHR(hr);
                if (e is FileNotFoundException)
                {
                    // invalid assembly name:
                    yield break;
                }
                else if (e != null)
                {
                    throw e;
                }
                else
                {
                    // for some reason it might happen that CreateAssemblyEnum returns non-zero HR that doesn't correspond to any exception:
                    throw new ArgumentException("Invalid assembly name".NeedsLocalization());
                }
            }

            while (true)
            {
                FusionAssemblyIdentity.IAssemblyName nameObject;

                hr = enumerator.GetNextAssembly(out applicationContext, out nameObject, 0);
                if (hr != 0)
                {
                    if (hr < 0)
                    {
                        Marshal.ThrowExceptionForHR(hr);
                    }

                    break;
                }

                if (!architectureFilter.IsDefault)
                {
                    var assemblyArchitecture = FusionAssemblyIdentity.GetProcessorArchitecture(nameObject);
                    if (!architectureFilter.Contains(assemblyArchitecture))
                    {
                        continue;
                    }
                }

                yield return nameObject;
            }
        }
 private static IEnumerable<AssemblyIdentity> GetAssemblyIdentities(
     FusionAssemblyIdentity.IAssemblyName partialName,
     ImmutableArray<ProcessorArchitecture> architectureFilter)
 {
     return from nameObject in GetAssemblyObjects(partialName, architectureFilter)
            select FusionAssemblyIdentity.ToAssemblyIdentity(nameObject);
 }
Esempio n. 9
0
        // Internal for testing.
        internal static IEnumerable <FusionAssemblyIdentity.IAssemblyName> GetAssemblyObjects(
            FusionAssemblyIdentity.IAssemblyName partialNameFilter,
            ImmutableArray <ProcessorArchitecture> architectureFilter)
        {
            IAssemblyEnum enumerator;

            FusionAssemblyIdentity.IApplicationContext applicationContext = null;

            int hr = CreateAssemblyEnum(out enumerator, applicationContext, partialNameFilter, ASM_CACHE.GAC, IntPtr.Zero);

            if (hr == S_FALSE)
            {
                // no assembly found
                yield break;
            }
            else if (hr != S_OK)
            {
                Exception e = Marshal.GetExceptionForHR(hr);
                if (e is FileNotFoundException)
                {
                    // invalid assembly name:
                    yield break;
                }
                else if (e != null)
                {
                    throw e;
                }
                else
                {
                    // for some reason it might happen that CreateAssemblyEnum returns non-zero HR that doesn't correspond to any exception:
#if SCRIPTING
                    throw new ArgumentException(Microsoft.CodeAnalysis.Scripting.ScriptingResources.InvalidAssemblyName);
#else
                    throw new ArgumentException(Microsoft.CodeAnalysis.WorkspaceDesktopResources.InvalidAssemblyName);
#endif
                }
            }

            while (true)
            {
                FusionAssemblyIdentity.IAssemblyName nameObject;

                hr = enumerator.GetNextAssembly(out applicationContext, out nameObject, 0);
                if (hr != 0)
                {
                    if (hr < 0)
                    {
                        Marshal.ThrowExceptionForHR(hr);
                    }

                    break;
                }

                if (!architectureFilter.IsDefault)
                {
                    var assemblyArchitecture = FusionAssemblyIdentity.GetProcessorArchitecture(nameObject);
                    if (!architectureFilter.Contains(assemblyArchitecture))
                    {
                        continue;
                    }
                }

                yield return(nameObject);
            }
        }
Esempio n. 10
0
 /// <summary>
 /// Enumerates assemblies in the GAC returning those that match given partial name and
 /// architecture.
 /// </summary>
 /// <param name="partialName">Optional partial name.</param>
 /// <param name="architectureFilter">Optional architecture filter.</param>
 public static IEnumerable <AssemblyIdentity> GetAssemblyIdentities(AssemblyName partialName, ImmutableArray <ProcessorArchitecture> architectureFilter = default(ImmutableArray <ProcessorArchitecture>))
 {
     return(GetAssemblyIdentities(FusionAssemblyIdentity.ToAssemblyNameObject(partialName), architectureFilter));
 }
Esempio n. 11
0
        // Internal for testing.
        internal static IEnumerable <FusionAssemblyIdentity.IAssemblyName> GetAssemblyObjects(
            FusionAssemblyIdentity.IAssemblyName partialNameFilter,
            Func <ProcessorArchitecture, bool> architectureFilter)
        {
            IAssemblyEnum enumerator;

            FusionAssemblyIdentity.IApplicationContext applicationContext = null;

            int hr = CreateAssemblyEnum(out enumerator, applicationContext, partialNameFilter, ASM_CACHE.GAC, IntPtr.Zero);

            if (hr == S_FALSE)
            {
                // no assembly found
                yield break;
            }
            else if (hr != S_OK)
            {
                Exception e = Marshal.GetExceptionForHR(hr);
                if (e is FileNotFoundException)
                {
                    // invalid assembly name:
                    yield break;
                }
                else if (e != null)
                {
                    throw e;
                }
                else
                {
                    // for some reason it might happen that CreateAssemblyEnum returns non-zero HR that doesn't correspond to any exception:
                    throw new ArgumentException("Invalid assembly name".NeedsLocalization());
                }
            }

            while (true)
            {
                FusionAssemblyIdentity.IAssemblyName nameObject;

                hr = enumerator.GetNextAssembly(out applicationContext, out nameObject, 0);
                if (hr != 0)
                {
                    if (hr < 0)
                    {
                        Marshal.ThrowExceptionForHR(hr);
                    }

                    break;
                }

                if (architectureFilter != null)
                {
                    var assemblyArchitecture = FusionAssemblyIdentity.GetProcessorArchitecture(nameObject);
                    if (!architectureFilter(assemblyArchitecture))
                    {
                        continue;
                    }
                }

                yield return(nameObject);
            }
        }
Esempio n. 12
0
 /// <summary>
 /// Enumerates assemblies in the GAC returning those that match given partial name and
 /// architecture.
 /// </summary>
 /// <param name="partialName">Optional partial name.</param>
 /// <param name="architectureFilter">Optional architecture filter.</param>
 public static IEnumerable <AssemblyIdentity> GetAssemblyIdentities(AssemblyName partialName, Func <ProcessorArchitecture, bool> architectureFilter = null)
 {
     return(GetAssemblyIdentities(FusionAssemblyIdentity.ToAssemblyNameObject(partialName), architectureFilter));
 }