Custom memory allocation in C# Part 18 — Hijacking methods on .NET 5 with modifying machine code

This is the eighteenth part of the Custom memory allocation series. For your convenience you can find other parts in the table of contents in Part 1 — Allocating object on a stack

Today we are going to see a rewritten way of hijacking method with machine code. It works in Windows and Linux, for both Debug and Release using .NET 5.

Tested with .NET 5.0.102 on Windows and .NET 5.0.401 on WSL2 Ubuntu 20.04. This is the output:

So we clearly see that the hacking works and that after multitiered compilation kicks in it no longer calls method but inlines it.