0

I have installed an external Nuget package and I am using one of its methods too often. Now, I want to call another method(I wrote it myself) right after every method call of that mentioned method. That would be like:

 MyExternalNugetPackage.Send(inputs); //varying inputs (in number, type, and values)
 MyOwnMethodCall();

Since I have got over thousands of "MyExternalNugetPackage.Send()", I don't wanna get in the trouble of adding "MyOwnMethodCall()" on each and every line. Is there any way I could somehow extend or add a functionality to "MyExternalNugetPackage.Send()" so that within itself it'd call "MyOwnMethodCall()" ?

Moji
  • 361
  • 6
  • 19
  • Maybe this will help: https://stackoverflow.com/questions/33075283/how-to-call-a-method-implicitly-after-every-method-call – MakePeaceGreatAgain Nov 30 '18 at 07:54
  • Everything you could do will assume some modification in your client-code on all those thousand of lines, e.g. some decorator. However why not search and replace? – MakePeaceGreatAgain Nov 30 '18 at 07:57
  • The point here is every " MyExternalNugetPackage.Send()" is different from one another parameter-wise. So I will have a ton of copy and replaces along the way. Also, this is actually a test (If it didn't work, I'd need to change it again). I was looking for some sort of event that would be called right after "myExternalNugetPackage.Send()" regardless of its input parameters – Moji Nov 30 '18 at 08:32
  • You can still search and replace by regex or similar. – MakePeaceGreatAgain Nov 30 '18 at 08:34

1 Answers1

1

You could make an extension method like

public static class Extensions
{
    public static void SendWithAction(this MyExternalNugetPackage myExternalNugetPackage, Action action)
    {
        myExternalNugetPackage.Send();
        action.Invoke();
    }
}

And then replace your Send() calls with your extension method like this

MyExternalNugetPackage.SendWithAction(MyOwnMethodCall);

Quite easy find replace to change them.

Either way, if you can not make some sort of hook like "AfterSend" that would be executed after Send() is complete, then you need to modify the code.

Janne Matikainen
  • 5,061
  • 15
  • 21
  • The problem is that the input parameter for each "myExternalNugetPackage.Send()" is different, so that way I will have to do a hell amount of copy and replace. I was looking for some sort of event that would be called right after "myExternalNugetPackage.Send()" regardless of its input parameters. – Moji Nov 30 '18 at 08:28