2

I'm working on a unit testing framework and part of that is intercepting calls to methods. I had a prototype using pointer manipulation on the pointers in MethodBase.MethodHandle but in some cases it would stop working depending on if the debugger was attached or not and in other cases it wouldn't work with functions (had it working for Actions though).

I suspect pointer manipulation is still a possible option but I don't know the memory layout or anything.

The fallback option I came up with is having the test method call my function with the function they want to call and then I generate a dynamic method, copying the IL and altering call instructions to call my methods instead. But it's not a great solution.

I looked through tens of sources but these are the ones that got me closest:

How do I dynamically edit/wrap a method at runtime?

Sellorio
  • 1,806
  • 1
  • 16
  • 32
  • 2
    What is your question? – thehennyy Jul 26 '19 at 11:30
  • Added to content for clarity. – Sellorio Jul 26 '19 at 11:42
  • Are you manually mocking the methods instead using existing mocking frameworks? Looks like an XY problem to me. Look into [asp.net core integration testing](https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-2.2#introduction-to-integration-tests) and more important [mocking nuget package](https://www.nuget.org/packages/moq/) – Cleptus Jul 26 '19 at 12:36
  • Mocking frameworks require the use of interfaces that are passed in. They can't test static methods or non-injectable/non-interfaced instance methods. They also don't support call-through since they have no knowledge of an implementation. – Sellorio Jul 27 '19 at 00:36
  • What’s wrong with using Harmony? It’s a mature and tested way of doing exactly what you want. – Andreas Pardeike Jul 30 '21 at 02:47

0 Answers0