Intercepting method called from a method of the same object

安稳与你 提交于 2019-12-13 07:43:10

问题


Here's the situation:

/// <summary>
/// A business logic class.
/// </summary>
public class BusinessClassWithInterceptor : BusinessClass, IBusinessClass
{
    /// <summary>
    /// Initializes a new instance of the <see cref="BusinessClassWithoutInterceptor"/> class.
    /// </summary>
    /// <param name="logger">The logger.</param>
    public BusinessClassWithInterceptor(Logger logger)
        : base(logger)
    {
    }

    /// <summary>
    /// Displays all cows.
    /// </summary>
    public void DisplayAllCows()
    {
        this.Logger.Write("Displaying all cows:");
        var repository = new CowRepository();
        foreach (CowEntity cow in repository.GetAllCows())
        {
            this.Logger.Write("    " + cow);
        }
    }

    /// <summary>
    /// Inserts a normande.
    /// </summary>
    public void InsertNormande(int id, string name)
    {
        this.DisplayAllCows();

        var repository = new CowRepository();
        repository.InsertCow(new CowEntity { Id = id, Name = name, Origin = CowOrigin.Normandie });
    }
}

With castle windsor, this class is configured to be intercepted with this interceptor:

/// <summary>
/// Interceptor for logging business methods.
/// </summary>
public class BusinessLogInterceptor : IInterceptor
{
    /// <summary>
    /// Intercepts the specified invocation.
    /// </summary>
    /// <param name="invocation">The invocation.</param>
    public void Intercept(IInvocation invocation)
    {
        Logger logger = ((IBusinessClass)invocation.InvocationTarget).Logger;

        var parameters = new StringBuilder();
        ParameterInfo[] methodParameters = invocation.Method.GetParameters();
        for (int index = 0; index < methodParameters.Length; index++)
        {
            parameters.AppendFormat("{0} = {1}", methodParameters[index].Name, invocation.Arguments[index]);
            if (index < methodParameters.Length - 1)
            {
                parameters.Append(", ");
            }
        }

        logger.Format("Calling {0}( {1} )", invocation.Method.Name, parameters.ToString());
        invocation.Proceed();
        logger.Format("Exiting {0}", invocation.Method.Name);
    }
}

The issue takes place during the call to InsertNormande. The call to InsertNormande is well intercepted, but the call to DisplayAllCows in InsertNormande is not intercepted...

It really bothers me.

Is there a way to achieve interception in this scenario ?


回答1:


I don't think there's an easy way of doing it... method calls that are internal to the class can't be intercepted, since they don't go through the proxy.

You could achieve logging of all methods by other means, such as an AOP framework like PostSharp



来源:https://stackoverflow.com/questions/4489127/intercepting-method-called-from-a-method-of-the-same-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!