From 9cff43238415571813e0780e49f362537603b9c7 Mon Sep 17 00:00:00 2001
From: puff <33184334+puff@users.noreply.github.com>
Date: Sun, 21 May 2023 23:10:31 -0400
Subject: [PATCH] Add renaming overridden methods automatically
---
README.md | 2 +-
.../Commands/RenameMemberCommand.cs | 34 +++++++++++++++++++
2 files changed, 35 insertions(+), 1 deletion(-)
diff --git a/README.md b/README.md
index ec5f88b..5e76368 100644
--- a/README.md
+++ b/README.md
@@ -4,7 +4,7 @@ dnSpy.Extension.EasyRename
A simple [dnSpy](https://github.com/dnSpyEx/dnSpy) extension for easily renaming members.
## Features
-- [ ] Rename overridden methods automatically.
+- [x] Rename overridden methods automatically.
- [x] Rename type when renaming a constructor method.
### Warnings
diff --git a/dnSpy.Extension.EasyRename/Commands/RenameMemberCommand.cs b/dnSpy.Extension.EasyRename/Commands/RenameMemberCommand.cs
index d8490a8..1913fa7 100644
--- a/dnSpy.Extension.EasyRename/Commands/RenameMemberCommand.cs
+++ b/dnSpy.Extension.EasyRename/Commands/RenameMemberCommand.cs
@@ -1,5 +1,6 @@
using System;
using System.ComponentModel.Composition;
+using System.Linq;
using dnlib.DotNet;
using dnSpy.Contracts.App;
using dnSpy.Contracts.Documents.Tabs;
@@ -48,13 +49,46 @@ public override void Execute(IMenuItemContext context)
if (isConstructor)
member.DeclaringType.Name = newName;
else
+ {
+ if (member is MethodDef method && (method.IsAbstract || method.IsVirtual))
+ {
+ foreach(var t in method.Module.GetTypes().Where(x => HasBaseType(x, method.DeclaringType, false)))
+ {
+ var @override = t.FindMethod(method.Name, method.MethodSig);
+ if (@override is { IsVirtual: true })
+ @override.Name = newName;
+ }
+ }
+
member.Name = newName;
+ }
var moduleDocNode = _documentTabService.DocumentTreeView.FindNode(member.Module)!;
_documentTabService.DocumentTreeView.TreeView.RefreshAllNodes();
_documentTabService.RefreshModifiedDocument(moduleDocNode.Document);
}
+ ///
+ /// Checks whether a type implements a specific base type.
+ ///
+ /// The type to check on.
+ /// The base type to check for.
+ /// Whether to check the type itself against the base type as well.
+ /// Whether the type implements a specific base type.
+ private static bool HasBaseType(ITypeDefOrRef type, ITypeDefOrRef baseType, bool @implicit = true)
+ {
+ var bt = @implicit ? type : type.GetBaseType();
+ while (bt is not null)
+ {
+ if (bt.Equals(baseType))
+ return true;
+
+ bt = bt.GetBaseType();
+ }
+
+ return false;
+ }
+
private static IMemberDef? GetMemberDef(IMenuItemContext context)
{
if (context.CreatorObject.Guid == new Guid(MenuConstants.GUIDOBJ_DOCUMENTVIEWERCONTROL_GUID))