之前為InterceptAttribute的上下文環境添加了“Intercept”屬性(InterceptProperty),正因為InterceptProperty繼承了IContributeObjectSink,所以我們要實現GetObjectSink(),繼而我們要創建一個繼承ImessageSink的類來作為返回值。
這樣就引發出了InterceptSink類的實現:
public class InterceptSink : IMessageSink { private IMessageSink nextSink = null; public IMessageSink NextSink { get { return nextSink; } } public InterceptSink(IMessageSink nextSink) { Console.WriteLine(" Call 'InterceptSink' - 'Constructor' "); this.nextSink = nextSink; } public IMessage SyncProcessMessage(IMessage msg) { Console.WriteLine("method_name: " + msg.Properties["__MethodName"].ToString()); IMessage returnMsg = nextSink.SyncProcessMessage(msg); return returnMsg; } public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink) { return null; } }
核心方法是:SyncProcessMessage(Imessage msg)
傳入參數msg中,我們可以找到調用對象方法的相應數據。