• If you have a mod, tool or prefab, please use the Resources section. Click Mods at the top of the forums.

How to use harmony to patch constructor?

Doux9Yuzuki

New member
I'm trying to patch a class's constructor using harmony. But an error occurs and the game log says that the method cannot be found. Can anyone help me?

my code:

[HarmonyPatch(typeof(DialogResponseQuest))]
[HarmonyPatch(new Type[] {typeof(string), typeof(string), typeof(string), typeof(string), typeof(Dialog), typeof(int), typeof(int)})]
public static class ShowQuestClearedPOI
{
public static void Postfix(DialogResponseQuest __instance)
{
// TODO
}
}




target class:

public class DialogResponseQuest : DialogResponse
{
public DialogResponseQuest(string _questID, string _nextStatementID, string _returnStatementID, string _type, Dialog _ownerDialog, int _listIndex = -1, int _tier = -1)
: base(_questID)
{
// original code
}
}




and the log:

Code:
2025-02-28T01:20:27 2.148 INF [MODS]   Initializing mod Doux9-ShowQuestClearedPOI
2025-02-28T01:20:27 2.148 INF [MODS]     Found ModAPI in ShowQuestClearedPOI.dll, creating instance
2025-02-28T01:20:27 2.148 INF Doux9桜月 Patch: Init
2025-02-28T01:20:27 2.158 INF [MODS][Harmony](Debug) ### Exception from user "Init", Harmony v2.13.0.0
### Original: NULL
### Patch class: ShowQuestClearedPOI
### System.ArgumentException: Undefined target method for patch method static DialogResponseQuest ShowQuestClearedPOI::Postfix(DialogResponseQuest __instance)
###   at HarmonyLib.PatchClassProcessor.PatchWithAttributes (System.Reflection.MethodBase& lastOriginal, System.Boolean unpatch) [0x0008b] in <7b787fece12a47a68027108671090713>:0 
###   at HarmonyLib.PatchClassProcessor.Patch () [0x00070] in <7b787fece12a47a68027108671090713>:0

2025-02-28T01:20:27 2.159 ERR [MODS]     Failed initializing ModAPI instance on mod 'Doux9-ShowQuestClearedPOI' from DLL 'ShowQuestClearedPOI.dll'
2025-02-28T01:20:27 2.160 EXC Patching exception in method null ---> Undefined target method for patch method static DialogResponseQuest ShowQuestClearedPOI::Postfix(DialogResponseQuest __instance)
  at HarmonyLib.PatchClassProcessor.PatchWithAttributes (System.Reflection.MethodBase& lastOriginal, System.Boolean unpatch) [0x0008b] in <7b787fece12a47a68027108671090713>:0 
  at HarmonyLib.PatchClassProcessor.Patch () [0x00070] in <7b787fece12a47a68027108671090713>:0 
Rethrow as HarmonyException: Patching exception in method null
  at HarmonyLib.PatchClassProcessor.ReportException (System.Exception exception, System.Reflection.MethodBase original) [0x0006c] in <7b787fece12a47a68027108671090713>:0 
  at HarmonyLib.PatchClassProcessor.Patch () [0x000a2] in <7b787fece12a47a68027108671090713>:0 
  at HarmonyLib.Harmony.<PatchAll>b__11_0 (System.Type type) [0x00007] in <7b787fece12a47a68027108671090713>:0 
  at HarmonyLib.CollectionExtensions.Do[T] (System.Collections.Generic.IEnumerable`1[T] sequence, System.Action`1[T] action) [0x00014] in <7b787fece12a47a68027108671090713>:0 
  at HarmonyLib.Harmony.PatchAll (System.Reflection.Assembly assembly) [0x00006] in <7b787fece12a47a68027108671090713>:0 
  at HarmonyLib.Harmony.PatchAll () [0x0001d] in <7b787fece12a47a68027108671090713>:0 
  at Init.InitMod (Mod _modInstance) [0x0002d] in E:\SteamLibrary\steamapps\common\7 Days To Die\Mods\ShowQuestClearedPOI\Harmony\Init.cs:11 
  at Mod.InitModCode () [0x0009d] in <0a824c04d551409fad5953ac8c5c40be>:0 
UnityEngine.StackTraceUtility:ExtractStringFromException(Object)
Log:Exception(Exception)
Mod:InitModCode()
ModManager:LoadMods()
GameManager:Awake()

 
Back
Top