using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Services;
using System.Runtime.Remoting.Activation;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
BusinessHandler handler = new BusinessHandler();
handler.DoSomething();
}
}
}
//业务层的类和方法,让它继承自上下文绑定类的基类
[MyInterceptor]
public class BusinessHandler : ContextBoundObject
{
[MyInterceptorMethod]
public void DoSomething()
{
MessageBox.Show("执行了方法本身!");
}
}
//贴在方法上的标签
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public sealed class MyInterceptorMethodAttribute : Attribute { }
//贴在类上的标签
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public sealed class MyInterceptorAttribute : ContextAttribute, IContributeObjectSink
{
public MyInterceptorAttribute()
: base("MyInterceptor")
{ }
//实现IContributeObjectSink接口当中的消息接收器接口
public IMessageSink GetObjectSink(MarshalByRefObject obj, IMessageSink next)
{
return new MyAopHandler(next);
}
}
//AOP方法处理类,实现了IMessageSink接口,以便返回给IContributeObjectSink接口的GetObjectSink方法
public sealed class MyAopHandler : IMessageSink
{
//下一个接收器
private IMessageSink nextSink;
public IMessageSink NextSink
{
get { return nextSink; }
}
public MyAopHandler(IMessageSink nextSink)
{
this.nextSink = nextSink;
}
//同步处理方法
public IMessage SyncProcessMessage(IMessage msg)
{
IMessage retMsg = null;
//方法调用消息接口
IMethodCallMessage call = msg as IMethodCallMessage;
//如果被调用的方法没打MyInterceptorMethodAttribute标签
if (call == null || (Attribute.GetCustomAttribute(call.MethodBase, typeof(MyInterceptorMethodAttribute))) == null)
{
retMsg = nextSink.SyncProcessMessage(msg);
}
//如果打了MyInterceptorMethodAttribute标签
else
{
MessageBox.Show("执行之前");
retMsg = nextSink.SyncProcessMessage(msg);
MessageBox.Show("执行之后");
}
return retMsg;
}
//异步处理方法(不需要)
public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink)
{
return null;
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Services;
using System.Runtime.Remoting.Activation;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
BusinessHandler handler = new BusinessHandler();
handler.DoSomething();
}
}
}
//业务层的类和方法,让它继承自上下文绑定类的基类
[MyInterceptor]
public class BusinessHandler : ContextBoundObject
{
[MyInterceptorMethod]
public void DoSomething()
{
MessageBox.Show("执行了方法本身!");
}
}
//贴在方法上的标签
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public sealed class MyInterceptorMethodAttribute : Attribute { }
//贴在类上的标签
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public sealed class MyInterceptorAttribute : ContextAttribute, IContributeObjectSink
{
public MyInterceptorAttribute()
: base("MyInterceptor")
{ }
//实现IContributeObjectSink接口当中的消息接收器接口
public IMessageSink GetObjectSink(MarshalByRefObject obj, IMessageSink next)
{
return new MyAopHandler(next);
}
}
//AOP方法处理类,实现了IMessageSink接口,以便返回给IContributeObjectSink接口的GetObjectSink方法
public sealed class MyAopHandler : IMessageSink
{
//下一个接收器
private IMessageSink nextSink;
public IMessageSink NextSink
{
get { return nextSink; }
}
public MyAopHandler(IMessageSink nextSink)
{
this.nextSink = nextSink;
}
//同步处理方法
public IMessage SyncProcessMessage(IMessage msg)
{
IMessage retMsg = null;
//方法调用消息接口
IMethodCallMessage call = msg as IMethodCallMessage;
//如果被调用的方法没打MyInterceptorMethodAttribute标签
if (call == null || (Attribute.GetCustomAttribute(call.MethodBase, typeof(MyInterceptorMethodAttribute))) == null)
{
retMsg = nextSink.SyncProcessMessage(msg);
}
//如果打了MyInterceptorMethodAttribute标签
else
{
MessageBox.Show("执行之前");
retMsg = nextSink.SyncProcessMessage(msg);
MessageBox.Show("执行之后");
}
return retMsg;
}
//异步处理方法(不需要)
public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink)
{
return null;
}
}