C#可通过多种方式实现AOP。1. 使用Castle DynamicProxy在运行时生成代理对象,通过IInterceptor拦截方法调用,实现日志、异常处理等切面逻辑;2. 结合自定义Attribute与动态代理,按特性标记决定是否应用切面,提升代码可读性;3. 使用PostSharp在编译期织入切面,性能好且语法简洁,但为商业框架需付费;4. 在简单场景下采用装饰器模式手动包装服务,实现轻量级AOP。选择方案应根据项目规模、性能需求与维护成本权衡。

AOP(面向切面编程)是一种编程范式,用于将横切关注点(如日志、权限验证、异常处理等)与核心业务逻辑分离。C# 本身不直接支持 AOP,但可以通过一些技术手段实现,比如使用 动态代理、特性(Attribute) 和第三方库(如 PostSharp 或 Castle DynamicProxy)来实现。
Casle DynamicProxy 是一个流行的开源库,可以在运行时为类生成代理对象,从而拦截方法调用,实现切面逻辑。
步骤如下:
示例代码:
using Castle.DynamicProxy;
<p>// 拦截器
public class LogInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
Console.WriteLine($"开始执行方法: {invocation.Method.Name}");</p><pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;"> try
{
invocation.Proceed(); // 执行原方法
}
catch
{
Console.WriteLine($"方法 {invocation.Method.Name} 发生异常");
throw;
}
finally
{
Console.WriteLine($"方法 {invocation.Method.Name} 执行完成");
}
}}
// 业务接口和实现 public interface IService { void DoWork(); }
public class Service : IService { public void DoWork() { Console.WriteLine("正在执行业务逻辑..."); } }
// 使用代理 var proxyGenerator = new ProxyGenerator(); var interceptor = new LogInterceptor(); var proxy = proxyGenerator.CreateInterfaceProxyWithTarget<IService>(new Service(), interceptor);
proxy.DoWork(); // 输出日志 + 业务逻辑
你可以自定义特性,标记需要切面处理的方法,然后在拦截器中判断是否应用逻辑。
示例:
[AttributeUsage(AttributeTargets.Method)]
public class LogAttribute : Attribute { }
<p>public class ConditionalLogInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
var hasLogAttr = invocation.Method.GetCustomAttributes(typeof(LogAttribute), false).Length > 0;
if (hasLogAttr)
{
Console.WriteLine($"[Log] 开始执行: {invocation.Method.Name}");
}</p><pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;"> invocation.Proceed();
if (hasLogAttr)
{
Console.WriteLine($"[Log] 完成执行: {invocation.Method.Name}");
}
}}
// 使用特性 public class UserService { [Log] public virtual void AddUser(string name) { Console.WriteLine($"添加用户: {name}"); } }
注意:DynamicProxy 要求被代理的方法必须是 virtual 或通过接口调用。
PostSharp 是一个商业 AOP 框架,它在编译期间将切面代码织入目标方法,性能更好,使用更简单。
安装 PostSharp NuGet 包后:
using PostSharp.Aspects;
<p>[Serializable]
public class LoggingAspect : OnMethodBoundaryAspect
{
public override void OnEntry(MethodExecutionArgs args)
{
Console.WriteLine($"进入方法: {args.Method.Name}");
}</p><pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">public override void OnExit(MethodExecutionArgs args)
{
Console.WriteLine($"退出方法: {args.Method.Name}");
}}
// 应用切面 [LoggingAspect] public void BusinessMethod() { Console.WriteLine("执行业务操作"); }
优点是写法简洁,缺点是付费且影响编译过程。
对于不需要复杂拦截的项目,可以用装饰器模式手动包装服务。
public class LoggingServiceDecorator : IService
{
private readonly IService _inner;
<pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">public LoggingServiceDecorator(IService inner) => _inner = inner;
public void DoWork()
{
Console.WriteLine("日志记录:开始");
_inner.DoWork();
Console.WriteLine("日志记录:结束");
}}
这种方式简单可控,适合小型项目。
基本上就这些常见的 C# 实现 AOP 的方式。选择哪种取决于你的需求:运行时代理灵活通用,PostSharp 更强大但需成本,装饰器最简单直接。根据项目规模和维护性权衡即可。
以上就是C# 如何实现 AOP 编程_C# AOP 面向切面编程教程的详细内容,更多请关注php中文网其它相关文章!
编程怎么学习?编程怎么入门?编程在哪学?编程怎么学才快?不用担心,这里为大家提供了编程速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号