dimanche 5 mai 2019

How can i get the name of an element that invoked an action using IInterceptor from Castle.DynamicProxy

So, pretty much what I'm trying to do is an aspect using the IInterceptor interface from the Castle.DynamicProxy namespace for logging certain element actions and save them to a file.

The implementation I have looks as describe below.

The interface type actions i'm intercepting look like this:

public interface IPageElement
{
   bool Visible { get; }
   void Select();
   ...
}

I have a class that its function is to serve as a factory for creating objects of type IPageElement. So, every time an factory method is called, I returned a proxy element by using CreateInterfaceProxyWithTarget<T>, this way:

return ProxyGenerator()
   .CreateInterfaceProxyWithTarget<IPageElement>(
      elementToProxy,
      new MyInterceptor(elementToProxy));

As you my guess, MyInterceptor class implements the IInterceptor interface from Castle.DynamicProxy. So, what I'd like to know is how to get the name of that IPageElement object that is being intercepted.

I've managed to get the method it calls, the params (if any), etc. by using the IInvocation parameter that Intercept method has:

invocation.Method; // for the method
invocation.Arguments;  // for the arguments of the method

Tried other of the methods/properties IInvocation has, but not luck.

Any help or suggestion is greatly appreciated :) Thanks in advanced.





Aucun commentaire:

Enregistrer un commentaire