public static Dictionary <string, List <Redirection> > GetFrameworkRedirections(List <AsmData> assembliesInGac, IAssemblyAnalyzerObserver observer) { string friendlyName = System.Threading.Thread.GetDomain().FriendlyName; string exeAssembly = System.Reflection.Assembly.GetEntryAssembly().FullName; AppDomainSetup setup = new AppDomainSetup(); setup.ApplicationBase = Environment.CurrentDirectory; setup.DisallowBindingRedirects = false; setup.DisallowCodeDownload = true; setup.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; AppDomain redirectionsScanDomain = AppDomain.CreateDomain("Framework Redirections"); redirectionsScanDomain.UnhandledException += new UnhandledExceptionEventHandler((sender, e) => ("An unhandled exception occured while parsing .NET Framework assemblies. We are sorry for any inconvenience caused.\r\nError message: " + ((Exception)e.ExceptionObject).Message).GetFormatter()); FrameworkRedirectionsScanner scanner1 = (FrameworkRedirectionsScanner)redirectionsScanDomain.CreateInstanceAndUnwrap(exeAssembly, typeof(FrameworkRedirectionsScanner).FullName); AppDomain.Unload(redirectionsScanDomain); return(new Dictionary <string, List <Redirection> >()); }
/// <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); }