private static SortedDictionary<Version, SortedDictionary<AssemblyNameExtension, string>> GenerateListOfAssembliesByRuntime(string strongName, GetAssemblyRuntimeVersion getRuntimeVersion, Version targetedRuntime, Microsoft.Build.Shared.FileExists fileExists, GetPathFromFusionName getPathFromFusionName, GetGacEnumerator getGacEnumerator, bool specificVersion) { Microsoft.Build.Shared.ErrorUtilities.VerifyThrowArgumentNull(targetedRuntime, "targetedRuntime"); IEnumerable<AssemblyNameExtension> enumerable = getGacEnumerator(strongName); SortedDictionary<Version, SortedDictionary<AssemblyNameExtension, string>> dictionary = new SortedDictionary<Version, SortedDictionary<AssemblyNameExtension, string>>(ReverseVersionGenericComparer.Comparer); if (enumerable != null) { foreach (AssemblyNameExtension extension in enumerable) { string str = getPathFromFusionName(extension.FullName); if (!string.IsNullOrEmpty(str) && fileExists(str)) { Version version = VersionUtilities.ConvertToVersion(getRuntimeVersion(str)); if ((version != null) && ((targetedRuntime.CompareTo(version) >= 0) || specificVersion)) { SortedDictionary<AssemblyNameExtension, string> dictionary2 = null; dictionary.TryGetValue(version, out dictionary2); if (dictionary2 == null) { dictionary2 = new SortedDictionary<AssemblyNameExtension, string>(AssemblyNameReverseVersionComparer.GenericComparer); dictionary.Add(version, dictionary2); } if (!dictionary2.ContainsKey(extension)) { dictionary2.Add(extension, str); } } } } } return dictionary; }
internal static string GetLocation(AssemblyNameExtension strongName, ProcessorArchitecture targetProcessorArchitecture, GetAssemblyRuntimeVersion getRuntimeVersion, Version targetedRuntimeVersion, bool fullFusionName, Microsoft.Build.Shared.FileExists fileExists, GetPathFromFusionName getPathFromFusionName, GetGacEnumerator getGacEnumerator, bool specificVersion) { string str = null; if (((strongName.GetPublicKeyToken() == null) || (strongName.GetPublicKeyToken().Length == 0)) && (strongName.FullName.IndexOf("PublicKeyToken", StringComparison.OrdinalIgnoreCase) != -1)) { return str; } getPathFromFusionName = getPathFromFusionName ?? pathFromFusionName; getGacEnumerator = getGacEnumerator ?? gacEnumerator; if (!strongName.HasProcessorArchitectureInFusionName) { if ((targetProcessorArchitecture != ProcessorArchitecture.MSIL) && (targetProcessorArchitecture != ProcessorArchitecture.None)) { string str2 = ResolveAssemblyReference.ProcessorArchitectureToString(targetProcessorArchitecture); if (fullFusionName) { str = CheckForFullFusionNameInGac(strongName, str2, getPathFromFusionName); } else { str = GetLocationImpl(strongName, str2, getRuntimeVersion, targetedRuntimeVersion, fileExists, getPathFromFusionName, getGacEnumerator, specificVersion); } if ((str != null) && (str.Length > 0)) { return str; } } if (fullFusionName) { str = CheckForFullFusionNameInGac(strongName, "MSIL", getPathFromFusionName); } else { str = GetLocationImpl(strongName, "MSIL", getRuntimeVersion, targetedRuntimeVersion, fileExists, getPathFromFusionName, getGacEnumerator, specificVersion); } if ((str != null) && (str.Length > 0)) { return str; } } if (fullFusionName) { str = CheckForFullFusionNameInGac(strongName, null, getPathFromFusionName); } else { str = GetLocationImpl(strongName, null, getRuntimeVersion, targetedRuntimeVersion, fileExists, getPathFromFusionName, getGacEnumerator, specificVersion); } if ((str != null) && (str.Length > 0)) { return str; } return null; }
/// <summary> /// Given a strong name, find its path in the GAC. /// </summary> /// <param name="strongName">The strong name.</param> /// <param name="targetProcessorArchitecture">Like x86 or IA64\AMD64.</param> /// <param name="getRuntimeVersion">Delegate to get the runtime version from a file path</param> /// <param name="targetedRuntimeVersion">What version of the runtime are we targeting</param> /// <param name="fullFusionName">Are we guranteed to have a full fusion name. This really can only happen if we have already resolved the assembly</param> /// <returns>The path to the assembly. Empty if none exists.</returns> internal static string GetLocation ( AssemblyNameExtension strongName, ProcessorArchitecture targetProcessorArchitecture, GetAssemblyRuntimeVersion getRuntimeVersion, Version targetedRuntimeVersion, bool fullFusionName, FileExists fileExists, GetPathFromFusionName getPathFromFusionName, GetGacEnumerator getGacEnumerator, bool specificVersion ) { return(GetLocation(null, strongName, targetProcessorArchitecture, getRuntimeVersion, targetedRuntimeVersion, fullFusionName, fileExists, getPathFromFusionName, getGacEnumerator, specificVersion)); }
/// <summary> /// Given a strong name generate the gac enumerator. /// </summary> internal static IEnumerable <AssemblyNameExtension> GetGacNativeEnumerator(string strongName) { IEnumerable <AssemblyNameExtension> gacEnumerator = null; try { // Will fail if the publickeyToken is null but will not fail if it is missing. gacEnumerator = new Microsoft.Build.Tasks.NativeMethods.AssemblyCacheEnum(strongName); } catch (FileLoadException) { // We could not handle the name passed in return(null); } return(gacEnumerator); }
/// <summary> /// Given a strong name, find its path in the GAC. /// </summary> /// <param name="strongName">The strong name.</param> /// <param name="targetProcessorArchitecture">Like x86 or IA64\AMD64.</param> /// <returns>The path to the assembly. Empty if none exists.</returns> private static string GetLocationImpl(AssemblyNameExtension assemblyName, string targetProcessorArchitecture, GetAssemblyRuntimeVersion getRuntimeVersion, Version targetedRuntime, FileExists fileExists, GetPathFromFusionName getPathFromFusionName, GetGacEnumerator getGacEnumerator, bool specificVersion) { // Extra checks for PInvoke-destined data. ErrorUtilities.VerifyThrowArgumentNull(assemblyName, "assemblyName"); ErrorUtilities.VerifyThrow(assemblyName.FullName != null, "Got a null assembly name fullname."); string strongName = assemblyName.FullName; if (targetProcessorArchitecture != null && !assemblyName.HasProcessorArchitectureInFusionName) { strongName += ", ProcessorArchitecture=" + targetProcessorArchitecture; } string assemblyPath = String.Empty; // Dictionary sorted by Version in reverse order, this will give the values enumeration the highest runtime version first. SortedDictionary<Version, SortedDictionary<AssemblyNameExtension, string>> assembliesByRuntime = GenerateListOfAssembliesByRuntime(strongName, getRuntimeVersion, targetedRuntime, fileExists, getPathFromFusionName, getGacEnumerator, specificVersion); if (assembliesByRuntime != null) { foreach (SortedDictionary<AssemblyNameExtension, string> runtimeBucket in assembliesByRuntime.Values) { // Grab the first element if there are one or more elements. This will give us the highest version assembly name. if (runtimeBucket.Count > 0) { foreach (KeyValuePair<AssemblyNameExtension, string> kvp in runtimeBucket) { assemblyPath = kvp.Value; break; } if (!String.IsNullOrEmpty(assemblyPath)) { break; } } } } return assemblyPath; }
/// <summary> /// Given a strong name, find its path in the GAC. /// </summary> /// <param name="strongName">The strong name.</param> /// <param name="targetProcessorArchitecture">Like x86 or IA64\AMD64.</param> /// <returns>The path to the assembly. Empty if none exists.</returns> private static string GetLocationImpl(AssemblyNameExtension assemblyName, string targetProcessorArchitecture, GetAssemblyRuntimeVersion getRuntimeVersion, Version targetedRuntime, FileExists fileExists, GetPathFromFusionName getPathFromFusionName, GetGacEnumerator getGacEnumerator, bool specificVersion) { // Extra checks for PInvoke-destined data. ErrorUtilities.VerifyThrowArgumentNull(assemblyName, "assemblyName"); ErrorUtilities.VerifyThrow(assemblyName.FullName != null, "Got a null assembly name fullname."); string strongName = assemblyName.FullName; if (targetProcessorArchitecture != null && !assemblyName.HasProcessorArchitectureInFusionName) { strongName += ", ProcessorArchitecture=" + targetProcessorArchitecture; } string assemblyPath = String.Empty; // Dictionary sorted by Version in reverse order, this will give the values enumeration the highest runtime version first. SortedDictionary <Version, SortedDictionary <AssemblyNameExtension, string> > assembliesByRuntime = GenerateListOfAssembliesByRuntime(strongName, getRuntimeVersion, targetedRuntime, fileExists, getPathFromFusionName, getGacEnumerator, specificVersion); if (assembliesByRuntime != null) { foreach (SortedDictionary <AssemblyNameExtension, string> runtimeBucket in assembliesByRuntime.Values) { // Grab the first element if there are one or more elements. This will give us the highest version assembly name. if (runtimeBucket.Count > 0) { foreach (KeyValuePair <AssemblyNameExtension, string> kvp in runtimeBucket) { assemblyPath = kvp.Value; break; } if (!String.IsNullOrEmpty(assemblyPath)) { break; } } } } return(assemblyPath); }
/// <summary> /// Given a strong name, find its path in the GAC. /// </summary> /// <param name="strongName">The strong name.</param> /// <param name="targetProcessorArchitecture">Like x86 or IA64\AMD64.</param> /// <param name="getRuntimeVersion">Delegate to get the runtime version from a file path</param> /// <param name="targetedRuntimeVersion">What version of the runtime are we targeting</param> /// <param name="fullFusionName">Are we guranteed to have a full fusion name. This really can only happen if we have already resolved the assembly</param> /// <returns>The path to the assembly. Empty if none exists.</returns> internal static string GetLocation ( IBuildEngine4 buildEngine, AssemblyNameExtension strongName, ProcessorArchitecture targetProcessorArchitecture, GetAssemblyRuntimeVersion getRuntimeVersion, Version targetedRuntimeVersion, bool fullFusionName, FileExists fileExists, GetPathFromFusionName getPathFromFusionName, GetGacEnumerator getGacEnumerator, bool specificVersion ) { ConcurrentDictionary <AssemblyNameExtension, string> fusionNameToResolvedPath = null; bool useGacRarCache = Environment.GetEnvironmentVariable("MSBUILDDISABLEGACRARCACHE") == null; if (buildEngine != null && useGacRarCache) { string key = "44d78b60-3bbe-48fe-9493-04119ebf515f" + "|" + targetProcessorArchitecture.ToString() + "|" + targetedRuntimeVersion.ToString() + "|" + fullFusionName.ToString() + "|" + specificVersion.ToString(); fusionNameToResolvedPath = buildEngine.GetRegisteredTaskObject(key, RegisteredTaskObjectLifetime.Build) as ConcurrentDictionary <AssemblyNameExtension, string>; if (fusionNameToResolvedPath == null) { fusionNameToResolvedPath = new ConcurrentDictionary <AssemblyNameExtension, string>(AssemblyNameComparer.GenericComparer); buildEngine.RegisterTaskObject(key, fusionNameToResolvedPath, RegisteredTaskObjectLifetime.Build, true /* dispose early ok*/); } else { if (fusionNameToResolvedPath.ContainsKey(strongName)) { string fusionName = null; fusionNameToResolvedPath.TryGetValue(strongName, out fusionName); return(fusionName); } } } // Optimize out the case where the public key token is null, if it is null it is not a strongly named assembly and CANNOT be in the gac. // also passing it would cause the gac enumeration method to throw an exception indicating the assembly is not a strongnamed assembly. string location = null; // If the publickeyToken is null and the publickeytoken is in the fusion name then this means we are passing in a null or empty PublicKeyToken and then this cannot possibly be in the gac. if ((strongName.GetPublicKeyToken() == null || strongName.GetPublicKeyToken().Length == 0) && strongName.FullName.IndexOf("PublicKeyToken", StringComparison.OrdinalIgnoreCase) != -1) { if (fusionNameToResolvedPath != null) { fusionNameToResolvedPath.TryAdd(strongName, location); } return(location); } // A delegate was not passed in to use the default one getPathFromFusionName = getPathFromFusionName ?? pathFromFusionName; // A delegate was not passed in to use the default one getGacEnumerator = getGacEnumerator ?? gacEnumerator; // If we have no processor architecture set then we can tryout a number of processor architectures. if (!strongName.HasProcessorArchitectureInFusionName) { if (targetProcessorArchitecture != ProcessorArchitecture.MSIL && targetProcessorArchitecture != ProcessorArchitecture.None) { string processorArchitecture = ResolveAssemblyReference.ProcessorArchitectureToString(targetProcessorArchitecture); // Try processor specific first. if (fullFusionName) { location = CheckForFullFusionNameInGac(strongName, processorArchitecture, getPathFromFusionName); } else { location = GetLocationImpl(strongName, processorArchitecture, getRuntimeVersion, targetedRuntimeVersion, fileExists, getPathFromFusionName, getGacEnumerator, specificVersion); } if (location != null && location.Length > 0) { if (fusionNameToResolvedPath != null) { fusionNameToResolvedPath.TryAdd(strongName, location); } return(location); } } // Next, try MSIL if (fullFusionName) { location = CheckForFullFusionNameInGac(strongName, "MSIL", getPathFromFusionName); } else { location = GetLocationImpl(strongName, "MSIL", getRuntimeVersion, targetedRuntimeVersion, fileExists, getPathFromFusionName, getGacEnumerator, specificVersion); } if (location != null && location.Length > 0) { if (fusionNameToResolvedPath != null) { fusionNameToResolvedPath.TryAdd(strongName, location); } return(location); } } // Next, try no processor architecure if (fullFusionName) { location = CheckForFullFusionNameInGac(strongName, null, getPathFromFusionName); } else { location = GetLocationImpl(strongName, null, getRuntimeVersion, targetedRuntimeVersion, fileExists, getPathFromFusionName, getGacEnumerator, specificVersion); } if (location != null && location.Length > 0) { if (fusionNameToResolvedPath != null) { fusionNameToResolvedPath.TryAdd(strongName, location); } return(location); } if (fusionNameToResolvedPath != null) { fusionNameToResolvedPath.TryAdd(strongName, null); } return(null); }
/// <summary> /// Enumerate the gac and generate a list of assemblies which match the strongname by runtime. /// </summary> private static SortedDictionary <Version, SortedDictionary <AssemblyNameExtension, string> > GenerateListOfAssembliesByRuntime(string strongName, GetAssemblyRuntimeVersion getRuntimeVersion, Version targetedRuntime, FileExists fileExists, GetPathFromFusionName getPathFromFusionName, GetGacEnumerator getGacEnumerator, bool specificVersion) { ErrorUtilities.VerifyThrowArgumentNull(targetedRuntime, "targetedRuntime"); IEnumerable <AssemblyNameExtension> gacEnum = getGacEnumerator(strongName); // Dictionary of Runtime version (sorted in reverse order) to a list of assemblies which are part of that runtime. This will allow us to pick the highest runtime and version first. SortedDictionary <Version, SortedDictionary <AssemblyNameExtension, string> > assembliesWithValidRuntimes = new SortedDictionary <Version, SortedDictionary <AssemblyNameExtension, string> >(ReverseVersionGenericComparer.Comparer); // Enumerate the gac values returned based on the partial or full fusion name. if (gacEnum != null) { foreach (AssemblyNameExtension gacAssembly in gacEnum) { // We only have a fusion name from the IAssemblyName interface we need to get the path to the assembly to resolve it and to check its runtime. string assemblyPath = getPathFromFusionName(gacAssembly.FullName); // Make sure we could get the path from the Fusion name and make sure the file actually exists. if (!String.IsNullOrEmpty(assemblyPath) && fileExists(assemblyPath)) { // Get the runtime version from the found assembly. string runtimeVersionRaw = getRuntimeVersion(assemblyPath); // Convert the runtime string to a version so we can properly compare them as per version object comparison rules. // We will accept version which are less than or equal to the targeted runtime. Version runtimeVersion = VersionUtilities.ConvertToVersion(runtimeVersionRaw); // Make sure the targeted runtime is greater than or equal to the runtime version of the assembly we got from the gac. if (runtimeVersion != null) { if (targetedRuntime.CompareTo(runtimeVersion) >= 0 || specificVersion) { SortedDictionary <AssemblyNameExtension, string> assembliesWithRuntime = null; assembliesWithValidRuntimes.TryGetValue(runtimeVersion, out assembliesWithRuntime); // Create a new list if one does not exist. if (assembliesWithRuntime == null) { assembliesWithRuntime = new SortedDictionary <AssemblyNameExtension, string>(AssemblyNameReverseVersionComparer.GenericComparer); assembliesWithValidRuntimes.Add(runtimeVersion, assembliesWithRuntime); } if (!assembliesWithRuntime.ContainsKey(gacAssembly)) { // Add the assembly to the list assembliesWithRuntime.Add(gacAssembly, assemblyPath); } } } } } } return(assembliesWithValidRuntimes); }
/// <summary> /// Given a strong name generate the gac enumerator. /// </summary> internal static IEnumerable<AssemblyNameExtension> GetGacNativeEnumerator(string strongName) { IEnumerable<AssemblyNameExtension> gacEnumerator = null; try { // Will fail if the publickeyToken is null but will not fail if it is missing. gacEnumerator = new Microsoft.Build.Tasks.NativeMethods.AssemblyCacheEnum(strongName); } catch (FileLoadException) { // We could not handle the name passed in return null; } return gacEnumerator; }
/// <summary> /// Given a strong name, find its path in the GAC. /// </summary> /// <param name="strongName">The strong name.</param> /// <param name="targetProcessorArchitecture">Like x86 or IA64\AMD64.</param> /// <param name="getRuntimeVersion">Delegate to get the runtime version from a file path</param> /// <param name="targetedRuntimeVersion">What version of the runtime are we targeting</param> /// <param name="fullFusionName">Are we guranteed to have a full fusion name. This really can only happen if we have already resolved the assembly</param> /// <returns>The path to the assembly. Empty if none exists.</returns> internal static string GetLocation ( IBuildEngine4 buildEngine, AssemblyNameExtension strongName, ProcessorArchitecture targetProcessorArchitecture, GetAssemblyRuntimeVersion getRuntimeVersion, Version targetedRuntimeVersion, bool fullFusionName, FileExists fileExists, GetPathFromFusionName getPathFromFusionName, GetGacEnumerator getGacEnumerator, bool specificVersion ) { ConcurrentDictionary<AssemblyNameExtension, string> fusionNameToResolvedPath = null; bool useGacRarCache = Environment.GetEnvironmentVariable("MSBUILDDISABLEGACRARCACHE") == null; if (buildEngine != null && useGacRarCache) { string key = "44d78b60-3bbe-48fe-9493-04119ebf515f" + "|" + targetProcessorArchitecture.ToString() + "|" + targetedRuntimeVersion.ToString() + "|" + fullFusionName.ToString() + "|" + specificVersion.ToString(); fusionNameToResolvedPath = buildEngine.GetRegisteredTaskObject(key, RegisteredTaskObjectLifetime.Build) as ConcurrentDictionary<AssemblyNameExtension, string>; if (fusionNameToResolvedPath == null) { fusionNameToResolvedPath = new ConcurrentDictionary<AssemblyNameExtension, string>(AssemblyNameComparer.GenericComparer); buildEngine.RegisterTaskObject(key, fusionNameToResolvedPath, RegisteredTaskObjectLifetime.Build, true /* dispose early ok*/); } else { if (fusionNameToResolvedPath.ContainsKey(strongName)) { string fusionName = null; fusionNameToResolvedPath.TryGetValue(strongName, out fusionName); return fusionName; } } } // Optimize out the case where the public key token is null, if it is null it is not a strongly named assembly and CANNOT be in the gac. // also passing it would cause the gac enumeration method to throw an exception indicating the assembly is not a strongnamed assembly. string location = null; // If the publickeyToken is null and the publickeytoken is in the fusion name then this means we are passing in a null or empty PublicKeyToken and then this cannot possibly be in the gac. if ((strongName.GetPublicKeyToken() == null || strongName.GetPublicKeyToken().Length == 0) && strongName.FullName.IndexOf("PublicKeyToken", StringComparison.OrdinalIgnoreCase) != -1) { if (fusionNameToResolvedPath != null) { fusionNameToResolvedPath.TryAdd(strongName, location); } return location; } // A delegate was not passed in to use the default one getPathFromFusionName = getPathFromFusionName ?? pathFromFusionName; // A delegate was not passed in to use the default one getGacEnumerator = getGacEnumerator ?? gacEnumerator; // If we have no processor architecture set then we can tryout a number of processor architectures. if (!strongName.HasProcessorArchitectureInFusionName) { if (targetProcessorArchitecture != ProcessorArchitecture.MSIL && targetProcessorArchitecture != ProcessorArchitecture.None) { string processorArchitecture = ResolveAssemblyReference.ProcessorArchitectureToString(targetProcessorArchitecture); // Try processor specific first. if (fullFusionName) { location = CheckForFullFusionNameInGac(strongName, processorArchitecture, getPathFromFusionName); } else { location = GetLocationImpl(strongName, processorArchitecture, getRuntimeVersion, targetedRuntimeVersion, fileExists, getPathFromFusionName, getGacEnumerator, specificVersion); } if (location != null && location.Length > 0) { if (fusionNameToResolvedPath != null) { fusionNameToResolvedPath.TryAdd(strongName, location); } return location; } } // Next, try MSIL if (fullFusionName) { location = CheckForFullFusionNameInGac(strongName, "MSIL", getPathFromFusionName); } else { location = GetLocationImpl(strongName, "MSIL", getRuntimeVersion, targetedRuntimeVersion, fileExists, getPathFromFusionName, getGacEnumerator, specificVersion); } if (location != null && location.Length > 0) { if (fusionNameToResolvedPath != null) { fusionNameToResolvedPath.TryAdd(strongName, location); } return location; } } // Next, try no processor architecure if (fullFusionName) { location = CheckForFullFusionNameInGac(strongName, null, getPathFromFusionName); } else { location = GetLocationImpl(strongName, null, getRuntimeVersion, targetedRuntimeVersion, fileExists, getPathFromFusionName, getGacEnumerator, specificVersion); } if (location != null && location.Length > 0) { if (fusionNameToResolvedPath != null) { fusionNameToResolvedPath.TryAdd(strongName, location); } return location; } if (fusionNameToResolvedPath != null) { fusionNameToResolvedPath.TryAdd(strongName, null); } return null; }
/// <summary> /// Given a strong name, find its path in the GAC. /// </summary> /// <param name="strongName">The strong name.</param> /// <param name="targetProcessorArchitecture">Like x86 or IA64\AMD64.</param> /// <param name="getRuntimeVersion">Delegate to get the runtime version from a file path</param> /// <param name="targetedRuntimeVersion">What version of the runtime are we targeting</param> /// <param name="fullFusionName">Are we guranteed to have a full fusion name. This really can only happen if we have already resolved the assembly</param> /// <returns>The path to the assembly. Empty if none exists.</returns> internal static string GetLocation ( AssemblyNameExtension strongName, ProcessorArchitecture targetProcessorArchitecture, GetAssemblyRuntimeVersion getRuntimeVersion, Version targetedRuntimeVersion, bool fullFusionName, FileExists fileExists, GetPathFromFusionName getPathFromFusionName, GetGacEnumerator getGacEnumerator, bool specificVersion ) { return GetLocation(null, strongName, targetProcessorArchitecture, getRuntimeVersion, targetedRuntimeVersion, fullFusionName, fileExists, getPathFromFusionName, getGacEnumerator, specificVersion); }
/// <summary> /// Enumerate the gac and generate a list of assemblies which match the strongname by runtime. /// </summary> private static SortedDictionary<Version, SortedDictionary<AssemblyNameExtension, string>> GenerateListOfAssembliesByRuntime(string strongName, GetAssemblyRuntimeVersion getRuntimeVersion, Version targetedRuntime, FileExists fileExists, GetPathFromFusionName getPathFromFusionName, GetGacEnumerator getGacEnumerator, bool specificVersion) { ErrorUtilities.VerifyThrowArgumentNull(targetedRuntime, "targetedRuntime"); IEnumerable<AssemblyNameExtension> gacEnum = getGacEnumerator(strongName); // Dictionary of Runtime version (sorted in reverse order) to a list of assemblies which are part of that runtime. This will allow us to pick the highest runtime and version first. SortedDictionary<Version, SortedDictionary<AssemblyNameExtension, string>> assembliesWithValidRuntimes = new SortedDictionary<Version, SortedDictionary<AssemblyNameExtension, string>>(ReverseVersionGenericComparer.Comparer); // Enumerate the gac values returned based on the partial or full fusion name. if (gacEnum != null) { foreach (AssemblyNameExtension gacAssembly in gacEnum) { // We only have a fusion name from the IAssemblyName interface we need to get the path to the assembly to resolve it and to check its runtime. string assemblyPath = getPathFromFusionName(gacAssembly.FullName); // Make sure we could get the path from the Fusion name and make sure the file actually exists. if (!String.IsNullOrEmpty(assemblyPath) && fileExists(assemblyPath)) { // Get the runtime version from the found assembly. string runtimeVersionRaw = getRuntimeVersion(assemblyPath); // Convert the runtime string to a version so we can properly compare them as per version object comparison rules. // We will accept version which are less than or equal to the targeted runtime. Version runtimeVersion = VersionUtilities.ConvertToVersion(runtimeVersionRaw); // Make sure the targeted runtime is greater than or equal to the runtime version of the assembly we got from the gac. if (runtimeVersion != null) { if (targetedRuntime.CompareTo(runtimeVersion) >= 0 || specificVersion) { SortedDictionary<AssemblyNameExtension, string> assembliesWithRuntime = null; assembliesWithValidRuntimes.TryGetValue(runtimeVersion, out assembliesWithRuntime); // Create a new list if one does not exist. if (assembliesWithRuntime == null) { assembliesWithRuntime = new SortedDictionary<AssemblyNameExtension, string>(AssemblyNameReverseVersionComparer.GenericComparer); assembliesWithValidRuntimes.Add(runtimeVersion, assembliesWithRuntime); } if (!assembliesWithRuntime.ContainsKey(gacAssembly)) { // Add the assembly to the list assembliesWithRuntime.Add(gacAssembly, assemblyPath); } } } } } } return assembliesWithValidRuntimes; }
internal static string GetLocation(AssemblyNameExtension strongName, ProcessorArchitecture targetProcessorArchitecture, GetAssemblyRuntimeVersion getRuntimeVersion, Version targetedRuntimeVersion, bool fullFusionName, Microsoft.Build.Shared.FileExists fileExists, GetPathFromFusionName getPathFromFusionName, GetGacEnumerator getGacEnumerator, bool specificVersion) { string str = null; if (((strongName.GetPublicKeyToken() == null) || (strongName.GetPublicKeyToken().Length == 0)) && (strongName.FullName.IndexOf("PublicKeyToken", StringComparison.OrdinalIgnoreCase) != -1)) { return(str); } getPathFromFusionName = getPathFromFusionName ?? pathFromFusionName; getGacEnumerator = getGacEnumerator ?? gacEnumerator; if (!strongName.HasProcessorArchitectureInFusionName) { if ((targetProcessorArchitecture != ProcessorArchitecture.MSIL) && (targetProcessorArchitecture != ProcessorArchitecture.None)) { string str2 = ResolveAssemblyReference.ProcessorArchitectureToString(targetProcessorArchitecture); if (fullFusionName) { str = CheckForFullFusionNameInGac(strongName, str2, getPathFromFusionName); } else { str = GetLocationImpl(strongName, str2, getRuntimeVersion, targetedRuntimeVersion, fileExists, getPathFromFusionName, getGacEnumerator, specificVersion); } if ((str != null) && (str.Length > 0)) { return(str); } } if (fullFusionName) { str = CheckForFullFusionNameInGac(strongName, "MSIL", getPathFromFusionName); } else { str = GetLocationImpl(strongName, "MSIL", getRuntimeVersion, targetedRuntimeVersion, fileExists, getPathFromFusionName, getGacEnumerator, specificVersion); } if ((str != null) && (str.Length > 0)) { return(str); } } if (fullFusionName) { str = CheckForFullFusionNameInGac(strongName, null, getPathFromFusionName); } else { str = GetLocationImpl(strongName, null, getRuntimeVersion, targetedRuntimeVersion, fileExists, getPathFromFusionName, getGacEnumerator, specificVersion); } if ((str != null) && (str.Length > 0)) { return(str); } return(null); }
private static SortedDictionary <Version, SortedDictionary <AssemblyNameExtension, string> > GenerateListOfAssembliesByRuntime(string strongName, GetAssemblyRuntimeVersion getRuntimeVersion, Version targetedRuntime, Microsoft.Build.Shared.FileExists fileExists, GetPathFromFusionName getPathFromFusionName, GetGacEnumerator getGacEnumerator, bool specificVersion) { Microsoft.Build.Shared.ErrorUtilities.VerifyThrowArgumentNull(targetedRuntime, "targetedRuntime"); IEnumerable <AssemblyNameExtension> enumerable = getGacEnumerator(strongName); SortedDictionary <Version, SortedDictionary <AssemblyNameExtension, string> > dictionary = new SortedDictionary <Version, SortedDictionary <AssemblyNameExtension, string> >(ReverseVersionGenericComparer.Comparer); if (enumerable != null) { foreach (AssemblyNameExtension extension in enumerable) { string str = getPathFromFusionName(extension.FullName); if (!string.IsNullOrEmpty(str) && fileExists(str)) { Version version = VersionUtilities.ConvertToVersion(getRuntimeVersion(str)); if ((version != null) && ((targetedRuntime.CompareTo(version) >= 0) || specificVersion)) { SortedDictionary <AssemblyNameExtension, string> dictionary2 = null; dictionary.TryGetValue(version, out dictionary2); if (dictionary2 == null) { dictionary2 = new SortedDictionary <AssemblyNameExtension, string>(AssemblyNameReverseVersionComparer.GenericComparer); dictionary.Add(version, dictionary2); } if (!dictionary2.ContainsKey(extension)) { dictionary2.Add(extension, str); } } } } } return(dictionary); }
private static string GetLocationImpl(AssemblyNameExtension assemblyName, string targetProcessorArchitecture, GetAssemblyRuntimeVersion getRuntimeVersion, Version targetedRuntime, Microsoft.Build.Shared.FileExists fileExists, GetPathFromFusionName getPathFromFusionName, GetGacEnumerator getGacEnumerator, bool specificVersion) { Microsoft.Build.Shared.ErrorUtilities.VerifyThrowArgumentNull(assemblyName, "assemblyName"); Microsoft.Build.Shared.ErrorUtilities.VerifyThrow(assemblyName.FullName != null, "Got a null assembly name fullname."); string fullName = assemblyName.FullName; if ((targetProcessorArchitecture != null) && !assemblyName.HasProcessorArchitectureInFusionName) { fullName = fullName + ", ProcessorArchitecture=" + targetProcessorArchitecture; } string str2 = string.Empty; SortedDictionary <Version, SortedDictionary <AssemblyNameExtension, string> > dictionary = GenerateListOfAssembliesByRuntime(fullName, getRuntimeVersion, targetedRuntime, fileExists, getPathFromFusionName, getGacEnumerator, specificVersion); if (dictionary != null) { foreach (SortedDictionary <AssemblyNameExtension, string> dictionary2 in dictionary.Values) { if (dictionary2.Count <= 0) { continue; } foreach (KeyValuePair <AssemblyNameExtension, string> pair in dictionary2) { str2 = pair.Value; break; } if (!string.IsNullOrEmpty(str2)) { return(str2); } } } return(str2); }
private static string GetLocationImpl(AssemblyNameExtension assemblyName, string targetProcessorArchitecture, GetAssemblyRuntimeVersion getRuntimeVersion, Version targetedRuntime, Microsoft.Build.Shared.FileExists fileExists, GetPathFromFusionName getPathFromFusionName, GetGacEnumerator getGacEnumerator, bool specificVersion) { Microsoft.Build.Shared.ErrorUtilities.VerifyThrowArgumentNull(assemblyName, "assemblyName"); Microsoft.Build.Shared.ErrorUtilities.VerifyThrow(assemblyName.FullName != null, "Got a null assembly name fullname."); string fullName = assemblyName.FullName; if ((targetProcessorArchitecture != null) && !assemblyName.HasProcessorArchitectureInFusionName) { fullName = fullName + ", ProcessorArchitecture=" + targetProcessorArchitecture; } string str2 = string.Empty; SortedDictionary<Version, SortedDictionary<AssemblyNameExtension, string>> dictionary = GenerateListOfAssembliesByRuntime(fullName, getRuntimeVersion, targetedRuntime, fileExists, getPathFromFusionName, getGacEnumerator, specificVersion); if (dictionary != null) { foreach (SortedDictionary<AssemblyNameExtension, string> dictionary2 in dictionary.Values) { if (dictionary2.Count <= 0) { continue; } foreach (KeyValuePair<AssemblyNameExtension, string> pair in dictionary2) { str2 = pair.Value; break; } if (!string.IsNullOrEmpty(str2)) { return str2; } } } return str2; }