之前为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中,我们可以找到调用对象方法的相应数据。
C#制作一个消息拦截器(intercept)3
时间: 2024-10-12 02:48:19