From e37d9ec8c70f58458ea6647c38cfa6f979364da4 Mon Sep 17 00:00:00 2001 From: Arne Kiesewetter Date: Mon, 3 Apr 2023 20:46:28 +0200 Subject: [PATCH] Add Delegate overloads and implicit casts to HarmonyMethod; Fixes #505 --- Harmony/Public/HarmonyMethod.cs | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/Harmony/Public/HarmonyMethod.cs b/Harmony/Public/HarmonyMethod.cs index 4ba83863..a9d60901 100644 --- a/Harmony/Public/HarmonyMethod.cs +++ b/Harmony/Public/HarmonyMethod.cs @@ -86,6 +86,13 @@ public HarmonyMethod(MethodInfo method) ImportMethod(method); } + /// Creates a patch from a given method + /// The original method + /// + public HarmonyMethod(Delegate @delegate) + : this(@delegate.Method) + { } + /// Creates a patch from a given method /// The original method /// The patch @@ -104,6 +111,17 @@ public HarmonyMethod(MethodInfo method, int priority = -1, string[] before = nul this.debug = debug; } + /// Creates a patch from a given method + /// The original method + /// The patch + /// A list of harmony IDs that should come after this patch + /// A list of harmony IDs that should come before this patch + /// Set to true to generate debug output + /// + public HarmonyMethod(Delegate @delegate, int priority = -1, string[] before = null, string[] after = null, bool? debug = null) + : this(@delegate.Method, priority, before, after, debug) + { } + /// Creates a patch from a given method /// The patch class/type /// The patch method name @@ -177,6 +195,22 @@ internal string Description() var aName = argumentTypes is object ? argumentTypes.Description() : "undefined"; return $"(class={cName}, methodname={mName}, type={tName}, args={aName})"; } + + /// Creates a patch from a given method + /// The original method + /// + public static implicit operator HarmonyMethod(MethodInfo method) + { + return new HarmonyMethod(method); + } + + /// Creates a patch from a given method + /// The original method + /// + public static implicit operator HarmonyMethod(Delegate @delegate) + { + return new HarmonyMethod(@delegate); + } } /// Annotation extensions