Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an example for patch factory methods #502

Merged
merged 1 commit into from Oct 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions Harmony/Documentation/articles/patching.md
Expand Up @@ -63,6 +63,25 @@ If you prefer manual patching, you can use any method name or class structure yo

![note] Patch methods _must_ be static but you can define them public or private. They cannot be dynamic methods but you can write static patch factory methods that return dynamic methods.

```csharp
[HarmonyPatch(...)]
class Patch
{
// the return type of factory methods can be either MethodInfo or DynamicMethod
[HarmonyPrefix]
static MethodInfo PrefixFactory(MethodBase originalMethod)
{
// return an instance of MethodInfo or an instance of DynamicMethod
}

[HarmonyPostfix]
static MethodInfo PostfixFactory(MethodBase originalMethod)
{
// return an instance of MethodInfo or an instance of DynamicMethod
}
}
```

### Method names

Manual patching knows four main patch types: **Prefix**, **Postfix**, **Transpiler** and **Finalizer**. If you use attributes for patching, you can also use the helper methods: **Prepare**, **TargetMethod**, **TargetMethods** and **Cleanup** as explained below.
Expand Down