Edgado D. Gonzalez
Edgado D. Gonzalez

Reputation: 1

Unity interception mechanism does not work between methods inside same class

I´m working with C# + Unity (2.1). Intercepting methods works fine if a call comes from outside, but between methods inside the same class only the first method is intercepted. For example:

    [InterceptorAttribute]
    public int A(int a, int b)
    {
         return B(a, b);
    }

    [InterceptorAttribute]
    public int B(int a, int b)
    {
         return a+b;
    }

The call to method B() is not intercepted. Can anyone help?

Upvotes: 0

Views: 610

Answers (1)

casablanca
casablanca

Reputation: 70721

If you look at how interception is implemented, it becomes clear as to why this happens. An interceptor is basically a proxy that wraps around the original object and forwards calls to it, in addition to calling any associated handlers:

public int A(int a, int b)
{
  callHandlers();
  return originalObject.A(a, b);
}

public int B(int a, int b)
{
  callHandlers();
  return originalObject.B(a, b);
}

Even though the two calls are individually intercepted, once originalMethod.A is invoked, the call to B will only invoke originalObject.B, not proxy.B.

Perhaps if you explain what you're using interception for, there may be a better solution to what you're trying to do.

Upvotes: 3

Related Questions