Example #1
0
 private static extern int CreateAssemblyEnum(out IAssemblyEnum ppEnum, AssemblyIdentity.IApplicationContext pAppCtx, AssemblyIdentity.IAssemblyName pName, ASM_CACHE dwFlags, IntPtr pvReserved);
Example #2
0
        // Internal for testing.
        internal static IEnumerable <AssemblyIdentity.IAssemblyName> GetAssemblyObjects(
            AssemblyIdentity.IAssemblyName partialNameFilter,
            Func <ProcessorArchitecture, bool> architectureFilter)
        {
            IAssemblyEnum enumerator;

            AssemblyIdentity.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(CodeAnalysisResources.InvalidAssemblyName);
                }
            }

            while (true)
            {
                AssemblyIdentity.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 = AssemblyIdentity.GetProcessorArchitecture(nameObject);
                    if (!architectureFilter(assemblyArchitecture))
                    {
                        continue;
                    }
                }

                yield return(nameObject);
            }
        }