internal static unsafe string GetAssemblyLocation(AssemblyIdentity.IAssemblyName nameObject) { // NAME | VERSION | CULTURE | PUBLIC_KEY_TOKEN | RETARGET | PROCESSORARCHITECTURE string fullName = AssemblyIdentity.GetDisplayName(nameObject, AssemblyIdentity.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); } }
private static IEnumerable <AssemblyIdentity> GetAssemblyIdentities( AssemblyIdentity.IAssemblyName partialName, Func <ProcessorArchitecture, bool> architectureFilter) { return(from nameObject in GetAssemblyObjects(partialName, architectureFilter) select AssemblyIdentity.ToAssemblyIdentity(nameObject)); }
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; AssemblyIdentity.IAssemblyName nameObject = AssemblyIdentity.ToAssemblyNameObject(displayName); if (nameObject == null) { return(null); } var candidates = GetAssemblyObjects(nameObject, architectureFilter); string cultureName = (preferredCulture != null && !preferredCulture.IsNeutralCulture) ? preferredCulture.Name : null; var bestMatch = AssemblyIdentity.GetBestMatch(candidates, cultureName); if (bestMatch == null) { return(null); } if (resolveLocation) { location = GetAssemblyLocation(bestMatch); } return(AssemblyIdentity.ToAssemblyIdentity(bestMatch)); }
private static extern int CreateAssemblyEnum(out IAssemblyEnum ppEnum, AssemblyIdentity.IApplicationContext pAppCtx, AssemblyIdentity.IAssemblyName pName, ASM_CACHE dwFlags, IntPtr pvReserved);
// 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); } }