From 577f025d5f47cdc6d62e6d333a23d3a211383862 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Fri, 13 Apr 2012 21:30:08 +0200 Subject: [PATCH] add some exception handling for startup and MEF exceptions --- ILSpy/App.xaml.cs | 18 ++++++++++++++++-- ILSpy/MainWindow.xaml.cs | 29 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/ILSpy/App.xaml.cs b/ILSpy/App.xaml.cs index b8659fc6e..9454924f3 100644 --- a/ILSpy/App.xaml.cs +++ b/ILSpy/App.xaml.cs @@ -17,6 +17,7 @@ // DEALINGS IN THE SOFTWARE. using System; +using System.Collections.Generic; using System.ComponentModel.Composition.Hosting; using System.Diagnostics; using System.IO; @@ -26,6 +27,7 @@ using System.Windows; using System.Windows.Documents; using System.Windows.Navigation; using System.Windows.Threading; + using ICSharpCode.ILSpy.Debugger.Services; using ICSharpCode.ILSpy.TextView; @@ -44,6 +46,14 @@ namespace ICSharpCode.ILSpy internal static CommandLineArguments CommandLineArguments; + internal static IList StartupExceptions = new List(); + + internal class ExceptionData + { + public Exception Exception; + public string PluginName; + } + public App() { var cmdArgs = Environment.GetCommandLineArgs().Skip(1); @@ -65,9 +75,13 @@ namespace ICSharpCode.ILSpy foreach (string plugin in Directory.GetFiles(appPath, "*.Plugin.dll")) { string shortName = Path.GetFileNameWithoutExtension(plugin); try { - catalog.Catalogs.Add(new AssemblyCatalog(Assembly.Load(shortName))); + var asm = Assembly.Load(shortName); + asm.GetTypes(); + catalog.Catalogs.Add(new AssemblyCatalog(asm)); } catch (Exception ex) { - MessageBox.Show(ex.ToString(), "Error loading plugin " + shortName); + // Cannot show MessageBox here, because WPF would crash with a XamlParseException + // Remember and show exceptions in text output, once MainWindow is properly initialized + StartupExceptions.Add(new ExceptionData { Exception = ex, PluginName = shortName }); } } diff --git a/ILSpy/MainWindow.xaml.cs b/ILSpy/MainWindow.xaml.cs index 57b61d1c4..39ba0ad81 100644 --- a/ILSpy/MainWindow.xaml.cs +++ b/ILSpy/MainWindow.xaml.cs @@ -31,6 +31,7 @@ using System.Windows.Input; using System.Windows.Interop; using System.Windows.Media; using System.Windows.Media.Imaging; +using ICSharpCode.Decompiler; using ICSharpCode.ILSpy.Debugger; using ICSharpCode.ILSpy.TextView; using ICSharpCode.ILSpy.TreeNodes; @@ -331,6 +332,34 @@ namespace ICSharpCode.ILSpy } NavigationCommands.Search.InputGestures.Add(new KeyGesture(Key.E, ModifierKeys.Control)); + + AvalonEditTextOutput output = new AvalonEditTextOutput(); + if (FormatExceptions(App.StartupExceptions.ToArray(), output)) + decompilerTextView.ShowText(output); + } + + bool FormatExceptions(App.ExceptionData[] exceptions, ITextOutput output) + { + if (exceptions.Length == 0) return false; + bool first = true; + + foreach (var item in exceptions) { + if (first) + first = false; + else + output.WriteLine("-------------------------------------------------"); + output.WriteLine("Error(s) loading plugin: " + item.PluginName); + if (item.Exception is System.Reflection.ReflectionTypeLoadException) { + var e = (System.Reflection.ReflectionTypeLoadException)item.Exception; + foreach (var ex in e.LoaderExceptions) { + output.WriteLine(ex.ToString()); + output.WriteLine(); + } + } else + output.WriteLine(item.Exception.ToString()); + } + + return true; } #region Update Check