lundi 15 mars 2021

How to cause method calls enter a different method first

I don't even know how to properly title my question so any suggestions and I will change it. The problem I have is this; I have an object which has some dispose work to do. Using the "using" keyword and scope is not possible because the object can be in use for a long time. I do not want to rely on the programmer to remember call Dispose() on these objects because he might forget. Instead, I would want all or some methods of the object to work like this:

  1. All the methods first go into some general function
  2. This general function calls the original method
  3. Depending on a bool param (for example) of the general method, the general method finished by maybe doing some cleanup work (disposing)

So if I have 10 methods, I would want them all to enter the same place, do their work, and then dispose. And I do not want to repeat the call to the dispose method in each of the 10 methods.

Example:

public void SomeBaseFunc(bool shouldDispose) //all methods first get drained here
{
//call whatever original func that the client called
//query bool param and dispose
}

Aucun commentaire:

Enregistrer un commentaire