From 69cad52cb4a7e8da07886ac0d1c8c63757431722 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sun, 6 Mar 2011 19:29:36 +0100 Subject: [PATCH] Hide compiler-generated enumerator classes when "yield return" decompilation is enabled. --- ICSharpCode.Decompiler/Ast/AstBuilder.cs | 32 +++++++++++++++--------- ILSpy/CSharpLanguage.cs | 2 +- ILSpy/DecompilationOptions.cs | 2 +- ILSpy/DecompilerSettingsPanel.xaml.cs | 10 ++++++++ ILSpy/MainWindow.xaml.cs | 11 +++++--- ILSpy/OptionsDialog.xaml.cs | 4 ++- 6 files changed, 43 insertions(+), 18 deletions(-) diff --git a/ICSharpCode.Decompiler/Ast/AstBuilder.cs b/ICSharpCode.Decompiler/Ast/AstBuilder.cs index 7c49b3915..0c249b6c6 100644 --- a/ICSharpCode.Decompiler/Ast/AstBuilder.cs +++ b/ICSharpCode.Decompiler/Ast/AstBuilder.cs @@ -33,19 +33,27 @@ namespace ICSharpCode.Decompiler.Ast this.context = context; } - public static bool MemberIsHidden(MemberReference member) + public static bool MemberIsHidden(MemberReference member, DecompilerSettings settings) { MethodDefinition method = member as MethodDefinition; - if (method != null && (method.IsGetter || method.IsSetter || method.IsAddOn || method.IsRemoveOn)) - return true; - if (method != null && method.Name.StartsWith("<", StringComparison.Ordinal) && method.IsCompilerGenerated()) - return true; + if (method != null) { + if (method.IsGetter || method.IsSetter || method.IsAddOn || method.IsRemoveOn) + return true; + if (settings.AnonymousMethods && method.Name.StartsWith("<", StringComparison.Ordinal) && method.IsCompilerGenerated()) + return true; + } TypeDefinition type = member as TypeDefinition; - if (type != null && type.DeclaringType != null && type.Name.StartsWith("<>c__DisplayClass", StringComparison.Ordinal) && type.IsCompilerGenerated()) - return true; + if (type != null && type.DeclaringType != null) { + if (settings.AnonymousMethods && type.Name.StartsWith("<>c__DisplayClass", StringComparison.Ordinal) && type.IsCompilerGenerated()) + return true; + if (settings.YieldReturn && YieldReturnDecompiler.IsCompilerGeneratorEnumerator(type)) + return true; + } FieldDefinition field = member as FieldDefinition; - if (field != null && field.Name.StartsWith("CS$<>", StringComparison.Ordinal) && field.IsCompilerGenerated()) - return true; + if (field != null) { + if (settings.AnonymousMethods && field.Name.StartsWith("CS$<>", StringComparison.Ordinal) && field.IsCompilerGenerated()) + return true; + } return false; } @@ -170,7 +178,7 @@ namespace ICSharpCode.Decompiler.Ast // Nested types foreach(TypeDefinition nestedTypeDef in typeDef.NestedTypes) { - if (MemberIsHidden(nestedTypeDef)) + if (MemberIsHidden(nestedTypeDef, context.Settings)) continue; astType.AddChild(CreateType(nestedTypeDef), TypeDeclaration.MemberRole); } @@ -487,7 +495,7 @@ namespace ICSharpCode.Decompiler.Ast { // Add fields foreach(FieldDefinition fieldDef in typeDef.Fields) { - if (MemberIsHidden(fieldDef)) continue; + if (MemberIsHidden(fieldDef, context.Settings)) continue; astType.AddChild(CreateField(fieldDef), TypeDeclaration.MemberRole); } @@ -510,7 +518,7 @@ namespace ICSharpCode.Decompiler.Ast // Add methods foreach(MethodDefinition methodDef in typeDef.Methods) { - if (methodDef.IsConstructor || MemberIsHidden(methodDef)) continue; + if (methodDef.IsConstructor || MemberIsHidden(methodDef, context.Settings)) continue; astType.AddChild(CreateMethod(methodDef), TypeDeclaration.MemberRole); } diff --git a/ILSpy/CSharpLanguage.cs b/ILSpy/CSharpLanguage.cs index 60fc12388..5c24eadb6 100644 --- a/ILSpy/CSharpLanguage.cs +++ b/ILSpy/CSharpLanguage.cs @@ -420,7 +420,7 @@ namespace ICSharpCode.ILSpy public override bool ShowMember(MemberReference member) { - return showAllMembers || !AstBuilder.MemberIsHidden(member); + return showAllMembers || !AstBuilder.MemberIsHidden(member, new DecompilationOptions().DecompilerSettings); } } } diff --git a/ILSpy/DecompilationOptions.cs b/ILSpy/DecompilationOptions.cs index bcd59af6b..162fbe29c 100644 --- a/ILSpy/DecompilationOptions.cs +++ b/ILSpy/DecompilationOptions.cs @@ -54,7 +54,7 @@ namespace ICSharpCode.ILSpy public DecompilationOptions() { - this.DecompilerSettings = DecompilerSettingsPanel.LoadDecompilerSettings(ILSpySettings.Load()); + this.DecompilerSettings = DecompilerSettingsPanel.CurrentDecompilerSettings; } } } diff --git a/ILSpy/DecompilerSettingsPanel.xaml.cs b/ILSpy/DecompilerSettingsPanel.xaml.cs index 694ad1040..03d45eef8 100644 --- a/ILSpy/DecompilerSettingsPanel.xaml.cs +++ b/ILSpy/DecompilerSettingsPanel.xaml.cs @@ -31,6 +31,14 @@ namespace ICSharpCode.ILSpy this.DataContext = LoadDecompilerSettings(settings); } + static DecompilerSettings currentDecompilerSettings; + + public static DecompilerSettings CurrentDecompilerSettings { + get { + return currentDecompilerSettings ?? (currentDecompilerSettings = LoadDecompilerSettings(ILSpySettings.Load())); + } + } + public static DecompilerSettings LoadDecompilerSettings(ILSpySettings settings) { XElement e = settings["DecompilerSettings"]; @@ -52,6 +60,8 @@ namespace ICSharpCode.ILSpy existingElement.ReplaceWith(section); else root.Add(section); + + currentDecompilerSettings = null; // invalidate cached settings } } } \ No newline at end of file diff --git a/ILSpy/MainWindow.xaml.cs b/ILSpy/MainWindow.xaml.cs index 90867ade9..88b28ff1a 100644 --- a/ILSpy/MainWindow.xaml.cs +++ b/ILSpy/MainWindow.xaml.cs @@ -281,15 +281,20 @@ namespace ICSharpCode.ILSpy } void filterSettings_PropertyChanged(object sender, PropertyChangedEventArgs e) + { + RefreshTreeViewFilter(); + if (e.PropertyName == "Language") { + TreeView_SelectionChanged(null, null); + } + } + + public void RefreshTreeViewFilter() { // filterSettings is mutable; but the ILSpyTreeNode filtering assumes that filter settings are immutable. // Thus, the main window will use one mutable instance (for data-binding), and assign a new clone to the ILSpyTreeNodes whenever the main // mutable instance changes. if (assemblyListTreeNode != null) assemblyListTreeNode.FilterSettings = sessionSettings.FilterSettings.Clone(); - if (e.PropertyName == "Language") { - TreeView_SelectionChanged(null, null); - } } internal AssemblyList AssemblyList { diff --git a/ILSpy/OptionsDialog.xaml.cs b/ILSpy/OptionsDialog.xaml.cs index dc87b88bf..fa975c82f 100644 --- a/ILSpy/OptionsDialog.xaml.cs +++ b/ILSpy/OptionsDialog.xaml.cs @@ -86,8 +86,10 @@ namespace ICSharpCode.ILSpy { OptionsDialog dlg = new OptionsDialog(); dlg.Owner = MainWindow.Instance; - if (dlg.ShowDialog() == true) + if (dlg.ShowDialog() == true) { + MainWindow.Instance.RefreshTreeViewFilter(); MainWindow.Instance.RefreshDecompiledView(); + } } } } \ No newline at end of file