C# 如何实现 AOP 编程_C# AOP 面向切面编程教程

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

c# 如何实现 aop 编程_c# aop 面向切面编程教程

AOP(面向切面编程)是一种编程范式,用于将横切关注点(如日志、权限验证、异常处理等)与核心业务逻辑分离。C# 本身不直接支持 AOP,但可以通过一些技术手段实现,比如使用 动态代理特性(Attribute) 和第三方库(如 PostSharpCastle DynamicProxy)来实现。

1. 使用 Castle DynamicProxy 实现 AOP

Casle DynamicProxy 是一个流行的开源库,可以在运行时为类生成代理对象,从而拦截方法调用,实现切面逻辑。

步骤如下:

  • 安装 NuGet 包:Castle.Core
  • 创建拦截器(实现 IInterceptor 接口)
  • 通过 ProxyGenerator 生成代理对象

示例代码:

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(); // 输出日志 + 业务逻辑

2. 使用特性(Attribute)结合动态代理增强可读性

你可以自定义特性,标记需要切面处理的方法,然后在拦截器中判断是否应用逻辑。

示例:

[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 或通过接口调用。

Vizard
Vizard

AI驱动的视频编辑器

Vizard 101
查看详情 Vizard

3. 使用 PostSharp(编译期 AOP)

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("执行业务操作"); }

优点是写法简洁,缺点是付费且影响编译过程。

4. 简单场景下使用装饰器模式模拟 AOP

对于不需要复杂拦截的项目,可以用装饰器模式手动包装服务。

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中文网其它相关文章!

编程速学教程(入门课程)
编程速学教程(入门课程)

编程怎么学习?编程怎么入门?编程在哪学?编程怎么学才快?不用担心,这里为大家提供了编程速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号