How do I replace a method implementation at runtime?

quentin-starin picture quentin-starin · Jun 17, 2010 · Viewed 8.5k times · Source

I'd like to have property getters and methods that I can decorate with my own custom attribute and based on the presence of that attribute replace the method bodies with a different implementation. Also, that different implementation will need to know the constructor arguments given to the custom attribute where it decorates the method.

This can obviously be done with AOP, like PostSharp or LinFu, but I'm wondering if there's a way to do this that does not involve a post-build processing step because adding that complicates the project more than I would prefer.

Answer

poizan42 picture poizan42 · Dec 7, 2016

There exists a couple of frameworks that allows you to dynamically change any method at runtime:

  • Harmony Free and Open Source (MIT)!
  • Prig: Free and Open Source (MIT), requires Visual Studio extension and running programs under a launcher, not updated since 2017.
  • Microsoft Fakes: Commercial, included in Visual Studio Enterprise (Premium and Ultimate for older versions) but not Community and Professional
  • Telerik JustMock: Commercial, a "lite" version is available
  • Typemock Isolator: Commercial