public static GACAssemblyName TryResolveGACAssembly(string displayName) { if (displayName.StartsWith(GlobalConstants.GACPrefix)) { displayName = displayName.Substring(4); } string[] split = displayName.Split(','); int index = -1; string culture = null, version = null, publicKeyToken = null, name = null; foreach (string part in split) { if (name == null && (index = part.IndexOf("=", StringComparison.OrdinalIgnoreCase)) == -1) { name = part.Trim(); } else { if (culture == null && (index = part.IndexOf("Culture=", StringComparison.OrdinalIgnoreCase)) != -1) { culture = part.Substring(index + 8).Trim(); } else if (version == null && (index = part.IndexOf("Version=", StringComparison.OrdinalIgnoreCase)) != -1) { version = part.Substring(index + 8).Trim(); } else if (publicKeyToken == null && (index = part.IndexOf("PublicKeyToken=", StringComparison.OrdinalIgnoreCase)) != -1) { publicKeyToken = part.Substring(index + 15).Trim(); } } } if (String.IsNullOrEmpty(name)) { return(null); } GACAssemblyName[] matchingName = GetGACAssemblies(name); if (matchingName.Length == 0) { return(null); } if (!String.IsNullOrEmpty(publicKeyToken)) { for (int i = 0; i < matchingName.Length; i++) { if (!String.Equals(matchingName[i].PublicKeyToken, publicKeyToken, StringComparison.OrdinalIgnoreCase)) { matchingName[i] = null; } } } if (!String.IsNullOrEmpty(culture)) { for (int i = 0; i < matchingName.Length; i++) { if (matchingName[i] != null && !String.Equals(matchingName[i].Culture, culture, StringComparison.OrdinalIgnoreCase)) { matchingName[i] = null; } } } GACAssemblyName winner = null; if (!String.IsNullOrEmpty(version)) { for (int i = 0; i < matchingName.Length; i++) { if (matchingName[i] != null) { if (!String.Equals(matchingName[i].Version, version, StringComparison.OrdinalIgnoreCase)) { matchingName[i] = null; } else { winner = matchingName[i]; } } } } else { Version latest = null; for (int i = 0; i < matchingName.Length; i++) { if (matchingName[i] != null) { if (latest == null) { winner = matchingName[i]; latest = new Version(winner.Version); } else { Version compare = new Version(matchingName[i].Version); if (latest < compare) { winner = matchingName[i]; latest = compare; } } } } } return(winner); }
public static string TryResolveGACAssembly(string name, string culture, string version, string publicKeyToken) { if (String.IsNullOrEmpty(name)) { return(null); } GACAssemblyName[] matchingName = GetGACAssemblies(name); if (matchingName.Length == 0) { return(null); } if (!String.IsNullOrEmpty(publicKeyToken)) { for (int i = 0; i < matchingName.Length; i++) { if (!String.Equals(matchingName[i].PublicKeyToken, publicKeyToken, StringComparison.OrdinalIgnoreCase)) { matchingName[i] = null; } } } if (!String.IsNullOrEmpty(culture)) { for (int i = 0; i < matchingName.Length; i++) { if (matchingName[i] != null && !String.Equals(matchingName[i].Culture, culture, StringComparison.OrdinalIgnoreCase)) { matchingName[i] = null; } } } GACAssemblyName winner = null; if (!String.IsNullOrEmpty(version)) { for (int i = 0; i < matchingName.Length; i++) { if (matchingName[i] != null) { if (!String.Equals(matchingName[i].Version, version, StringComparison.OrdinalIgnoreCase)) { matchingName[i] = null; } else { winner = matchingName[i]; } } } } else { Version latest = null; for (int i = 0; i < matchingName.Length; i++) { if (matchingName[i] != null) { if (latest == null) { winner = matchingName[i]; latest = new Version(winner.Version); } else { Version compare = new Version(matchingName[i].Version); if (latest < compare) { winner = matchingName[i]; latest = compare; } } } } } if (winner == null) { return(null); } return(winner.ToString()); }