From ffd7d5c71df8da3dbf9d55e9a7e9fa91419de1fe Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Mon, 9 Jul 2018 21:38:02 +0200 Subject: [PATCH] Fix analyzers for properties and events, add missing file. --- ILSpy/Analyzers/AnalyzeContextMenuEntry.cs | 4 +-- .../Builtin/TypeExposedByAnalyzer.cs | 3 ++ .../TreeNodes/AnalyzedEventTreeNode.cs | 5 ++- .../TreeNodes/AnalyzedPropertyTreeNode.cs | 4 +-- ILSpy/TypeExtensionMethodsAnalyzer.cs | 33 +++++++++++++++++++ 5 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 ILSpy/TypeExtensionMethodsAnalyzer.cs diff --git a/ILSpy/Analyzers/AnalyzeContextMenuEntry.cs b/ILSpy/Analyzers/AnalyzeContextMenuEntry.cs index 221ad2748..88e480272 100644 --- a/ILSpy/Analyzers/AnalyzeContextMenuEntry.cs +++ b/ILSpy/Analyzers/AnalyzeContextMenuEntry.cs @@ -79,10 +79,10 @@ namespace ICSharpCode.ILSpy.Analyzers AnalyzerTreeView.Instance.ShowOrFocus(new AnalyzedMethodTreeNode(md)); break; case IProperty pd: - //AnalyzerTreeView.Instance.ShowOrFocus(new AnalyzedPropertyTreeNode(pd)); + AnalyzerTreeView.Instance.ShowOrFocus(new AnalyzedPropertyTreeNode(pd)); break; case IEvent ed: - //AnalyzerTreeView.Instance.ShowOrFocus(new AnalyzedEventTreeNode(ed)); + AnalyzerTreeView.Instance.ShowOrFocus(new AnalyzedEventTreeNode(ed)); break; default: throw new NotSupportedException(); diff --git a/ILSpy/Analyzers/Builtin/TypeExposedByAnalyzer.cs b/ILSpy/Analyzers/Builtin/TypeExposedByAnalyzer.cs index 4a30f3bbc..4a3abe2da 100644 --- a/ILSpy/Analyzers/Builtin/TypeExposedByAnalyzer.cs +++ b/ILSpy/Analyzers/Builtin/TypeExposedByAnalyzer.cs @@ -8,6 +8,9 @@ using ICSharpCode.Decompiler.TypeSystem; namespace ICSharpCode.ILSpy.Analyzers.Builtin { + /// + /// Finds all entities that expose a type. + /// [Export(typeof(IAnalyzer))] class TypeExposedByAnalyzer : ITypeDefinitionAnalyzer { diff --git a/ILSpy/Analyzers/TreeNodes/AnalyzedEventTreeNode.cs b/ILSpy/Analyzers/TreeNodes/AnalyzedEventTreeNode.cs index 453d97a6b..021b76abf 100644 --- a/ILSpy/Analyzers/TreeNodes/AnalyzedEventTreeNode.cs +++ b/ILSpy/Analyzers/TreeNodes/AnalyzedEventTreeNode.cs @@ -44,10 +44,9 @@ namespace ICSharpCode.ILSpy.Analyzers.TreeNodes protected override void LoadChildren() { - if (analyzedEvent.AddAccessor != null) + if (analyzedEvent.CanAdd) this.Children.Add(new AnalyzedAccessorTreeNode(analyzedEvent.AddAccessor, "add")); - - if (analyzedEvent.RemoveAccessor != null) + if (analyzedEvent.CanRemove) this.Children.Add(new AnalyzedAccessorTreeNode(analyzedEvent.RemoveAccessor, "remove")); //foreach (var accessor in analyzedEvent.OtherMethods) diff --git a/ILSpy/Analyzers/TreeNodes/AnalyzedPropertyTreeNode.cs b/ILSpy/Analyzers/TreeNodes/AnalyzedPropertyTreeNode.cs index 60f2f42d0..5537aa5dc 100644 --- a/ILSpy/Analyzers/TreeNodes/AnalyzedPropertyTreeNode.cs +++ b/ILSpy/Analyzers/TreeNodes/AnalyzedPropertyTreeNode.cs @@ -41,9 +41,9 @@ namespace ICSharpCode.ILSpy.Analyzers.TreeNodes protected override void LoadChildren() { - if (!analyzedProperty.CanGet) + if (analyzedProperty.CanGet) this.Children.Add(new AnalyzedAccessorTreeNode(analyzedProperty.Getter, "get")); - if (!analyzedProperty.CanSet) + if (analyzedProperty.CanSet) this.Children.Add(new AnalyzedAccessorTreeNode(analyzedProperty.Setter, "set")); //foreach (var accessor in analyzedProperty.OtherMethods) // this.Children.Add(new AnalyzedPropertyAccessorTreeNode(accessor, null)); diff --git a/ILSpy/TypeExtensionMethodsAnalyzer.cs b/ILSpy/TypeExtensionMethodsAnalyzer.cs new file mode 100644 index 000000000..6e8fa10e4 --- /dev/null +++ b/ILSpy/TypeExtensionMethodsAnalyzer.cs @@ -0,0 +1,33 @@ +using System.Collections.Generic; +using System.ComponentModel.Composition; +using ICSharpCode.Decompiler.TypeSystem; + +namespace ICSharpCode.ILSpy.Analyzers.Builtin +{ + /// + /// Finds all extension methods defined for a type. + /// + [Export(typeof(IAnalyzer))] + class TypeExtensionMethodsAnalyzer : ITypeDefinitionAnalyzer + { + public string Text => "Extension Methods"; + + public bool Show(ITypeDefinition entity) => !entity.IsStatic; + + public IEnumerable Analyze(ITypeDefinition analyzedType, ITypeDefinition type, AnalyzerContext context) + { + if (!type.HasExtensionMethods) + yield break; + + foreach (IMethod method in type.Methods) { + if (!method.IsExtensionMethod) continue; + + var firstParamType = method.Parameters[0].Type.GetDefinition(); + if (firstParamType != null && + firstParamType.MetadataToken == analyzedType.MetadataToken && + firstParamType.ParentAssembly.PEFile == analyzedType.ParentAssembly.PEFile) + yield return method; + } + } + } +}