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

AOP(面向切面编程)是一种编程范式,用于将横切关注点(如日志、权限验证、异常处理等)与核心业务逻辑分离。C# 本身不直接支持 AOP,但可以通过一些技术手段实现,比如使用 动态代理、特性(Attribute) 和第三方库(如 PostSharp 或 Castle DynamicProxy)来实现。
1. 使用 Castle DynamicProxy 实现 AOP
Casle DynamicProxy 是一个流行的开源库,可以在运行时为类生成代理对象,从而拦截方法调用,实现切面逻辑。
步骤如下:
- 安装 NuGet 包:Castle.Core
- 创建拦截器(实现 IInterceptor 接口)
- 通过 ProxyGenerator 生成代理对象
示例代码:
using Castle.DynamicProxy;// 拦截器 public class LogInterceptor : IInterceptor { public void Intercept(IInvocation invocation) { Console.WriteLine($"开始执行方法: {invocation.Method.Name}");
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
(new Service(), interceptor); proxy.DoWork(); // 输出日志 + 业务逻辑
2. 使用特性(Attribute)结合动态代理增强可读性
你可以自定义特性,标记需要切面处理的方法,然后在拦截器中判断是否应用逻辑。
示例:
[AttributeUsage(AttributeTargets.Method)] public class LogAttribute : Attribute { }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}"); }
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 或通过接口调用。
3. 使用 PostSharp(编译期 AOP)
PostSharp 是一个商业 AOP 框架,它在编译期间将切面代码织入目标方法,性能更好,使用更简单。
安装 PostSharp NuGet 包后:
using PostSharp.Aspects;[Serializable] public class LoggingAspect : OnMethodBoundaryAspect { public override void OnEntry(MethodExecutionArgs args) { Console.WriteLine($"进入方法: {args.Method.Name}"); }
public override void OnExit(MethodExecutionArgs args) { Console.WriteLine($"退出方法: {args.Method.Name}"); }}
// 应用切面 [LoggingAspect] public void BusinessMethod() { Console.WriteLine("执行业务操作"); }
优点是写法简洁,缺点是付费且影响编译过程。
4. 简单场景下使用装饰器模式模拟 AOP
对于不需要复杂拦截的项目,可以用装饰器模式手动包装服务。
public class LoggingServiceDecorator : IService { private readonly IService _inner;public LoggingServiceDecorator(IService inner) => _inner = inner; public void DoWork() { Console.WriteLine("日志记录:开始"); _inner.DoWork(); Console.WriteLine("日志记录:结束"); }}
这种方式简单可控,适合小型项目。
基本上就这些常见的 C# 实现 AOP 的方式。选择哪种取决于你的需求:运行时代理灵活通用,PostSharp 更强大但需成本,装饰器最简单直接。根据项目规模和维护性权衡即可。











