/// <summary> /// Recupera os redirecionamentos das versões. /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static Dictionary <string, List <Redirection> > GetVersionRedirections(string fileName) { var ret = new Dictionary <string, List <Redirection> >(); var config = new System.Xml.XmlDocument(); config.Load(fileName); foreach (System.Xml.XmlNode dependentAssemblyTag in config.GetElementsByTagName("dependentAssembly")) { if (((dependentAssemblyTag.ParentNode.Name == "assemblyBinding") && (dependentAssemblyTag.ParentNode.ParentNode != null)) && (dependentAssemblyTag.ParentNode.ParentNode.Name == "runtime")) { Redirection red = new Redirection(); foreach (System.Xml.XmlNode node in dependentAssemblyTag.ChildNodes) { if (node.Name == "assemblyIdentity") { var name = new System.Reflection.AssemblyName(node.Attributes["name"].Value); if (node.Attributes["processorArchitecture"] != null) { name.ProcessorArchitecture = (System.Reflection.ProcessorArchitecture)Enum.Parse(typeof(System.Reflection.ProcessorArchitecture), node.Attributes["processorArchitecture"].Value, true); } red.AssemblyIdentity = name; continue; } if (node.Name == "bindingRedirect") { BindingRedirect redirect = new BindingRedirect(); if (node.Attributes["oldVersion"] != null) { System.Xml.XmlAttribute attr = node.Attributes["oldVersion"]; if (attr.Value.Contains("-")) { string[] versions = attr.Value.Split(new char[] { '-' }); redirect.OldVersionMin = new Version(versions[0]); redirect.OldVersionMax = new Version(versions[1]); } else { redirect.OldVersionMax = new Version(attr.Value); redirect.OldVersionMin = new Version(attr.Value); } } if (node.Attributes["newVersion"] != null) { redirect.NewVersion = new Version(node.Attributes["newVersion"].Value); } red.BindingRedirection = redirect; } } if (ret.ContainsKey(red.AssemblyIdentity.Name)) { ret[red.AssemblyIdentity.Name].Add(red); } else { var aux = new List <Redirection>(); aux.Add(red); ret.Add(red.AssemblyIdentity.Name, aux); } } } if (ret.Count > 0) { return(ret); } return(null); }
/// <summary> /// Recupera os redirecionamentos do Framework. /// </summary> /// <param name="assembliesInGac"></param> /// <param name="progressDialog"></param> /// <returns></returns> public Dictionary <string, List <Redirection> > GetFrameworkRedirections(List <AsmData> assembliesInGac, IAssemblyAnalyzerObserver progressDialog) { Dictionary <string, List <Redirection> > redirections = new Dictionary <string, List <Redirection> >(); try { progressDialog.ReportProgress(0, "Checking .NET Framework libraries...".GetFormatter()); int assembliesCount = 0; var upgrades = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\.NETFramework\Policy\Upgrades"); if (upgrades == null) { return(redirections); } var bindingRedirects = new List <BindingRedirect>(); foreach (string targetVersion in upgrades.GetValueNames()) { string sourceVersion = upgrades.GetValue(targetVersion) as string; BindingRedirect redirect = new BindingRedirect(); redirect.NewVersion = new Version(targetVersion); if (sourceVersion.Contains("-")) { string[] versions = sourceVersion.Split(new char[] { '-' }); redirect.OldVersionMin = new Version(versions[0]); redirect.OldVersionMax = new Version(versions[1]); } else { redirect.OldVersionMax = new Version(sourceVersion); redirect.OldVersionMin = new Version(sourceVersion); } bindingRedirects.Add(redirect); } upgrades.Close(); foreach (AsmData assemblyDescription in assembliesInGac) { System.Reflection.Assembly asm = null; try { asm = System.Reflection.Assembly.Load(assemblyDescription.Name); } catch (Exception) { continue; } var assemblyName = asm.GetName(false); if (!redirections.ContainsKey(assemblyName.Name)) { object[] attributes = null; try { attributes = asm.GetCustomAttributes(typeof(System.Reflection.AssemblyProductAttribute), false); } catch (Exception) { } if ((attributes != null) && (attributes.Length > 0)) { var productAttribute = attributes[0] as System.Reflection.AssemblyProductAttribute; if ((productAttribute != null) && (productAttribute.Product == "Microsoft\x00ae .NET Framework")) { foreach (BindingRedirect bindingRedirect in bindingRedirects) { Redirection redirection = new Redirection(); redirection.AssemblyIdentity = assemblyName; redirection.BindingRedirection = bindingRedirect; if (assemblyName.Version <= redirection.BindingRedirection.NewVersion) { redirection.BindingRedirection.NewVersion = assemblyName.Version; } if (redirections.ContainsKey(redirection.AssemblyIdentity.Name)) { redirections[redirection.AssemblyIdentity.Name].Add(redirection); } else { var aux = new List <Redirection>(); aux.Add(redirection); redirections.Add(redirection.AssemblyIdentity.Name, aux); } } } } assembliesCount++; progressDialog.ReportProgress((int)((100.0 * assembliesCount) / ((double)assembliesInGac.Count)), "Checking .NET Framework libraries...".GetFormatter()); if (progressDialog.CancellationPending) { redirections.Clear(); return(redirections); } } } } catch (Exception) { } return(redirections); }