Monkey Patching in C#

Is it possible to extend or modify the code of a C# class at run-time?

No it is not possible to do this in .NET. You could write derived classes and override methods (if they are virtual) but you cannot modify an existing class. Just imagine if what you were asking was possible: you could modify the behavior of some existing system classes like System.String.

You may also take a look at Extension methods to add functionality to an existing class.


You can add functionality, but you cannot change or remove functionality.


You can extend classes by adding extra methods, but you cannot override them because added methods have always lower priority than existing ones.

For more info, check Extension Methods in C# Programming Guide.