AOP的簡單示例
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;
}
}
相關文章
- AOP的簡單實現
- AOP的簡單介紹
- startViewTransition的簡單示例View
- Java使用ObjectMapper的簡單示例JavaObjectAPP
- spring之AOP的概念及簡單案例Spring
- SpringBoot切面Aop的demo簡單講解Spring Boot
- 第70篇 AOP的簡單介紹
- [譯]ViewModels:一個簡單的示例View
- 寫一個簡單的 Facade 示例
- gpt給出的operator簡單示例GPT
- C# superSocket簡單示例C#
- Spring AOP就是這麼簡單啦Spring
- 一個簡單的 indexedDB 應用示例Index
- linux下定時任務的簡單示例Linux
- Spring Boot實際專案用簡單的AOPSpring Boot
- sed命令簡單使用示例分享
- Windows守護程式簡單示例Windows
- Python 裝飾器簡單示例Python
- 簡易版的Spring框架之AOP簡單實現(對我來說不簡單啊)Spring框架
- Spring【AOP模組】就是這麼簡單Spring
- 幾種常用設計模式的簡單示例設計模式
- spring aop實現簡單的許可權控制功能Spring
- Spark Streaming簡單入門(示例+原理)Spark
- WebGL簡易教程(一):第一個簡單示例Web
- 【GO】Elasticsearch的簡單寫入和讀取示例GoElasticsearch
- 最簡單的SpringBoot示例之.yml配置檔案Spring Boot
- Django應用建立到啟動的簡單示例Django
- AOP簡介
- Docker(3):Dockerfile介紹及簡單示例Docker
- Android MVP模式--簡單實用示例 BMIAndroidMVP模式
- Kafka簡單示例以及常用命令Kafka
- Hbase、Hive、Impala資料同步簡單示例Hive
- Spring Cloud Alibaba(12)---Gatway概述、簡單示例SpringCloud
- Spring Boot與Kafka + kafdrop結合使用的簡單示例Spring BootKafka
- [譯]WebAssembly: 帶有程式碼示例的簡單介紹Web
- 從零開始的簡單光線追蹤示例
- 專案管理工具Maven的簡單配置示例專案管理Maven
- 簡單介紹Go 字串比較的實現示例Go字串
- 簡單的python web專案的docker-compose.yml 示例PythonWebDocker