/// <param name="codeBaseUri">Uri pointing to the assembly</param> public static bool Contains(Uri codeBaseUri) { if(codeBaseUri == null) { Debug.Fail("codeBaseUri == null"); return false; } lock(GlobalAssemblyCache.Lock) { if(!GlobalAssemblyCache.FusionLoaded) { GlobalAssemblyCache.FusionLoaded = true; System.Reflection.Assembly systemAssembly = typeof(object).Assembly; //^ assume systemAssembly != null && systemAssembly.Location != null; string dir = Path.GetDirectoryName(systemAssembly.Location); //^ assume dir != null; GlobalAssemblyCache.LoadLibrary(Path.Combine(dir, "fusion.dll")); } IAssemblyEnum assemblyEnum; int rc = GlobalAssemblyCache.CreateAssemblyEnum(out assemblyEnum, null, null, GAC, 0); if(rc < 0 || assemblyEnum == null) return false; IApplicationContext applicationContext; IAssemblyName currentName; while(assemblyEnum.GetNextAssembly(out applicationContext, out currentName, 0) == 0) { //^ assume currentName != null; AssemblyName assemblyName = new AssemblyName(currentName); string scheme = codeBaseUri.Scheme; if(scheme != null && assemblyName.CodeBase.StartsWith(scheme)) { try { Uri foundUri = new Uri(assemblyName.CodeBase); if(codeBaseUri.Equals(foundUri)) return true; } catch(Exception) { } } } return false; } }
/// <summary> /// Returns the original location of the corresponding assembly if available, otherwise returns the /// location of the shadow copy. If the corresponding assembly is not in the GAC, null is returned. /// </summary> public static string GetLocation(AssemblyReference assemblyReference) { if(assemblyReference == null) { Debug.Fail("assemblyReference == null"); return null; } lock(GlobalAssemblyCache.Lock) { if(!GlobalAssemblyCache.FusionLoaded) { GlobalAssemblyCache.FusionLoaded = true; System.Reflection.Assembly systemAssembly = typeof(object).Assembly; //^ assume systemAssembly != null && systemAssembly.Location != null; string dir = Path.GetDirectoryName(systemAssembly.Location); //^ assume dir != null; GlobalAssemblyCache.LoadLibrary(Path.Combine(dir, "fusion.dll")); } IAssemblyEnum assemblyEnum; CreateAssemblyEnum(out assemblyEnum, null, null, GAC, 0); if(assemblyEnum == null) return null; IApplicationContext applicationContext; IAssemblyName currentName; while(assemblyEnum.GetNextAssembly(out applicationContext, out currentName, 0) == 0) { //^ assume currentName != null; AssemblyName aName = new AssemblyName(currentName); if(assemblyReference.Matches(aName.Name, aName.Version, aName.Culture, aName.PublicKeyToken)) { string codeBase = aName.CodeBase; if(codeBase != null && codeBase.StartsWith("file:///")) return codeBase.Substring(8); return aName.GetLocation(); } } return null; } }