Our great sponsors
-
InfluxDB
Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.
If you’re unfamiliar with the term Monkey patching, it’s a process of changing some code “on-the-fly”, where the author of the code didn’t necessarily want that behaviour. It isn’t, however, something you can do in C# (or .NET) in general. There are some libraries like Harmony that can do it partially, but are based around your code running and doing the patching of the libraries. The general premise is that you want to redirect a call between a method the code thought it was going to call, and some other method. This can be incredibly useful if want to change/fix the functionality of something you don’t control. Khalid (@buhakmeh) has a post on some of these approaches here, and we’re going to look at an alternative that has a very narrow use case.
There is sample code here, and each commit shows the different phases. In this basic example, we’ll redirect our own code that is using System.Console to our new PrefixConsole. Our new class prepends “WithPrefix: ” to the front of all our console WriteLines.