前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C# 12 拦截器 Interceptors

C# 12 拦截器 Interceptors

作者头像
Chester Chen
发布2024-03-25 14:00:53
890
发布2024-03-25 14:00:53
举报
文章被收录于专栏:chester技术分享chester技术分享

拦截器Interceptors是一种可以在编译时以声明方式替换原有应用的方法。

这种替换是通过让Interceptors声明它拦截的调用的源位置来实现的。

您可以使用拦截器作为源生成器的一部分进行修改,而不是向现有源编译添加代码。

演示

使用 .NET 8 创建一个控制台应用程序。并在PropertyGroup中添加以下配置.。需要将其中WebApplication6替换为自己的命名空间。

代码语言:javascript
复制
<InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);WebApplication6</InterceptorsPreviewNamespaces>

然后在单独的文件中创建InterceptsLocationAttribute。其命名空间必须是System.Runtime.CompilerServices,而不是应用程序的命名空间。

代码语言:javascript
复制
namespace System.Runtime.CompilerServices
{
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public sealed class InterceptsLocationAttribute(string filePath, int line, int character) : Attribute
    {
    }
}

该属性包含三个参数。

  • filePath是您要拦截的文件的路径。
  • line是您要拦截的代码行。
  • character是您要拦截的代码字符位置。

接着来创建一个具有三种方法的类,模拟新增/查询用户作为示例:

代码语言:javascript
复制
public class GetUserService
{
    // This method will not be intercepted;
    public void GetUserName()
    {
        Console.WriteLine("GetUserName");
    }

    // This method will be intercepted;
    public void AddUser()
    {
        Console.WriteLine("AddUser");
    }

    // This method will not be intercepted;
    public void DeleteUser()
    {
        Console.WriteLine("DeleteUser");
    }
}

在 Program.cs 文件中,我创建了此类的一个实例,并创建了对这三个方法中每一个的调用。输出如下所示:

代码语言:javascript
复制
var userService = new GetUserService();

userService.GetUserName();
userService.AddUser();
userService.DeleteUser();

现在让我们创建拦截类。该类必须遵循以下规则:

  • 一定是一个static类。
  • 必须是我们要拦截的类的扩展方法。
  • 必须具有该InterceptsLocation属性,其中包含我们要拦截的文件路径的值以及行号和字符号。
代码语言:javascript
复制
using System.Runtime.CompilerServices;

namespace WebApplication6
{
    public static class InterceptUserService
    {
        [InterceptsLocation(
    filePath: @"D:\demo\test\ConsoleApp1\WebApplication6\Program.cs",
    line: 14,
    character: 25)]
        public static void InterceptMethodAddUser(this GetUserService example)
        {
            Console.WriteLine("Interceptor is here!");
        }
    }
}

在此示例中,将拦截AddUser方法,并且将执行InterceptMethodAddUser方法,而不是执行方法AddUser。

filePath可以按以下方式获取

行号和字符号可以按以下方式获取

现在运行代码,方法AddUser将被拦截,并且不会被执行,而是实际执行拦截器方法,以下是输出:

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-03-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 chester技术分享 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 演示
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档