How do I execute a method at the end of each method of my C #

5

You have a parent class and a daughter, and would like to execute a particular method, such as an event, always at the end of each method call for the child classes. How can I do it?

    
asked by anonymous 23.06.2014 / 13:40

4 answers

8

This is called Aspect Oriented Programming (#

In AOP, the methods are decorated with aspects. Generally, there are 3 types of aspects: aspects that are executed before the method, during the method, or after of the method.

Unfortunately, C # does not support this paradigm natively.

However there are 2 ways to use AOP in C #.

Option 1 - PostSharp

PostSharp is a tool that allows you to memorize methods with attributes that represent aspects of these methods. During compilation, PostSharp injects into the body of the method itself.

But PostSharp is a paid tool (with 45 days trial). If this is not an option, I recommend option 2:

Option 2 - Interceptors / Castle Dynamic Proxy

Castle DynamicProxy allows you to dynamically create (at runtime) a type that acts as a proxy of another type. The proxy intercepts all calls made to the target, and allows you to add logic involving the call.

In your case, as you want to call a method (for example, CleanUp ) after all calls to the object, the interceptor would look something like this:

public class Interceptor : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        //invocar metodo no target
        invocation.Proceed();

        //invocar aspecto
        MinhaClass obj = invocation.InvocationTarget as MinhaClass;
        if(obj != null)
            obj.CleanUp();
    }
}

The CleanUp method will be called after all calls to any instance of the MinhaClass instance (as long as the instance is decorated with the proxy).

Castle DynamicProxy works best when used in conjunction with Castle Windsor as a dependency injection tool. Thus, all instances of the class will be automatically decorated with the proxy.

About DynamicProxy:

This project is very popular. E 'used as a basis in:

  • NHibernate

      

    For example, NHibernate, an object / relational mapper uses DynamicProxy to provide lazy loading of data without the domain model being aware of this functionality.

  • various mocking frameworks (such as Moq and RhinoMocks) to dynamically generate mocks.

  • IoC containers (Inversion of control / dependency injection) such as Castle Windsor and Ninject.

  • In my company we occasionally use to add aspects of "logging" to some classes, so that all calls to their methods are logged in the EventViewer in debug mode.

    With DynamicProxy, we can 1) completely separate the logging of the business logic and 2) set the logging on one site, and then apply the interceptor to several other classes (DRY).

    More:

  • Introduction to AOP with Castle
  • DynamicProxy
  • 25.06.2014 / 00:35
    1

    Using a generic method in parent class:

    public void Metodo<T>(T t) where t: IInterfaceClasseFilha
    {
        MetodoAntes();
        Metodo(t);
        MetodoDepois();
    }
    
        
    23.06.2014 / 17:44
    0

    I do not understand your question for sure, but class with events is this way as an example below:

    Classes with event:

    public class Pai : IDisposable
    {
        public delegate void Acao(int numbers);
        public event Acao Actions;
        public void Execute()
        {
            Actions(1000);
        }
    
        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }
    }
    
    public class Filho : Pai
    {
    
    }
    

    Using:

    static void filho_Actions(int numbers)
    {
        System.Console.WriteLine(numbers);
    }
    
    Filho filho = new Filho();
    filho.Actions += filho_Actions;
    filho.Execute();
    

    Output:

        
    23.06.2014 / 18:00
    0

    Use the try / finaly build. The code in the finaly construct will always be executed even if an exception occurs in your class, so you should anticipate the treatment that should be given properly.

        
    24.06.2014 / 13:07