0

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 a 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.

u32i64
  • 2,384
  • 3
  • 22
  • 36
Carlos Parra
  • 957
  • 9
  • 19
  • What is "the name of that IPageElement object", your interface has no name? If you want the name of the class implementing your interface, you've got the elementToProxy passed into your interceptor, or you could use `IInvocation.InvocationTarget` and just call `GetType().Name`. – Jonathon Rossi May 06 '19 at 11:14
  • hey @JonathonRossi in this case, I want the name of the element that's of type `IPageElement`. i.e.: `public IPageElement myElement { get; set; }` so, when i call `myElement.Select()` in my interceptor, I'd like to get 'myElement' as a string value. – Carlos Parra May 06 '19 at 16:56
  • Oh, you want the variable name holding the reference to the element object. DynamicProxy can only do stuff you can also do with normal code, so it isn't possible unless the class with the `myElement` property returned an `IPageElement` that captured that property name and stored it, which could then be used when any of the members are called. – Jonathon Rossi May 07 '19 at 00:19

0 Answers0