0

I am intercepting the methods on an interface because I want to be sure my pre-invoke code is run for all public methods of any given implementation. I know I could do method interception, but that makes configuration harder and risks new methods in the future not getting configured.

At runtime I am able to get the implemented class from the IInvocation argument by calling GetType on invocation.Request.Target. What I can't find is how to get which method is being called.

Once I have the class type, I could probably query the method by name, but then I have to get in to querying on parameters as well in case there's overloads and I'd rather avoid that if possible.

Ultimately I am trying to pull the attributes off the calling method. So if there's a shortcut, that would work too.

EDIT: I know that I can put my attributes on the interface members (I am doing that now as a work around), but that cannot be the final solution in my case.

EDIT 2: Requested code sample:

public class PublicBusinessInterceptor : SimpleInterceptor
{
    protected override void BeforeInvoke(IInvocation invocation)
    {
        var interfaceMemberType = invocation.Request.Target.GetType();
    }
}

In the code sample above, interfaceMemberType is, as the variable name implies, the type of the interface member. I need the type of the implemented member that is being called in this context. The implemented member type has the attributes that I need. The interface member type does not.

Steven
  • 166,672
  • 24
  • 332
  • 435
Mark Bostleman
  • 2,185
  • 3
  • 25
  • 32
  • 1
    what was the question again? a little source code example would help understanding. since this sounds interesting – FrankM Dec 07 '17 at 14:58
  • The title is the question and I'm not sure how to clarify it any further other than the fact that when you intercept an interface, the MethodInfo object you get is the interface's member - not the implemented member that is actually getting called. Therefore, if you want attributes (or anything for that matter) from the implemented member you need to somehow get the MethodInfo for that. – Mark Bostleman Dec 07 '17 at 15:06
  • 1
    Little code sample costs thousands of verbal explanations – Evk Dec 07 '17 at 15:09
  • Doesn't `IInvocation` has `Method` property? – Evk Dec 07 '17 at 15:25
  • @Evk yes, IInvocation has a property of type MethodInfo that is named "Method". When you configure the interception of an interface, that MethodInfo is that of the *interface* member, not that of the *implemented* member. – Mark Bostleman Dec 07 '17 at 15:29
  • There is `IInvocation.MethodInvocationTarget` also, which I think is what you are looking for – Evk Dec 07 '17 at 15:39
  • @Evk I am using Ninject.Extensions.Interception 3.3. MethodInvocationTarget is not a member of IInvocation and I can't find anything like that anywhere in the object graph. However, if it did exist, it would pretty much be exactly what I need. – Mark Bostleman Dec 07 '17 at 15:48
  • Well that's why we ask for normal code example (not like you have now). You tagged question with "castle-dynamicproxy" and `IInvocation` there has `MehtodInvocationTarget` which does exactly what you need. – Evk Dec 07 '17 at 15:50
  • @Evk What should I do to make my code example "normal"? This is the code I am working with. How would I make it clearer? – Mark Bostleman Dec 07 '17 at 15:53
  • You just need to make it more complete. Now it's just one line. You can include full class definition (so at least it's visible what this class inherits\which interfaces implements). How you are using it also won't hurt. – Evk Dec 07 '17 at 15:54
  • Agreed, and done. The fact that I am overriding SimpleInterceptor is now visible. – Mark Bostleman Dec 07 '17 at 15:57
  • Well, seems there is no other way to do that aside from what you described in question (searching method by name and argument types). – Evk Dec 07 '17 at 16:17

1 Answers1

0

Not sure I understand your question, but, as far as I understand, this may be a starting basis :

 public class MyInterceptor : IInterceptor
 {
     public void Intercept(IInvocation invocation)
     {
         // The intercepted method
         var method = invocation.Request.Method;

         // the method of the concrete type which is intercepted
         var actualMethod = invocation.Request.Target.GetType()
             .GetMethod(method.Name, method.GetParameters().Select(p => p.ParameterType).ToArray());

         // the desired attribute
         var myAttribute = actualMethod.GetCustomAttributes<MyAttribute>().FirstOrDefault();

         if (myAttribute == null)
        {
            invocation.Proceed();
        }
        else
        {
          // Do stuff
        }
     }
jbl
  • 15,179
  • 3
  • 34
  • 101