NET 8.0 C#的拦截器

在网上集各家之所长的基础上写的,大部分在其他文章里都能找到,方便自己看
如果写拦截器的的时候报签名的错误,多是参数不匹配,就是下图框起来的地方问题在这里插入图片描述
配置文件.csproj里加

<PropertyGroup>
    <Features>InterceptorsPreview</Features>
    <LangVersion>preview</LangVersion>      
    <InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);ConsoleAppNET8Test</InterceptorsPreviewNamespaces> 
    <InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);D</InterceptorsPreviewNamespaces>
  </PropertyGroup>

Program.cs文件内容如下

using System.Runtime.CompilerServices;
#region 调试1成功
//B.M(); // What the Fudge?!
//B.M(); // Original
//class B
//{
//    public static void M() => Console.WriteLine("Original");
//}
//namespace D
//{
//    public static class D
//    {
//        [InterceptsLocation(filePath: "Program.cs", line: 3, character: 3)]
//        public static void M() => Console.WriteLine("What the Fudge?!");
//    }
//}
#endregion

#region 调试2成功
//var myclass = new MyClass();
//myclass.Print("测试");
//myclass.Print("测试");
//public class MyClass
//{
//    public void Print(string s)
//    {
//        Console.WriteLine($"MyClass.Print({s})");
//    }
//}
//namespace ConsoleAppNET8Test
//{
//    public static class MyClassIntercepts
//    {
//        [InterceptsLocation(filePath: @"C:\Users\AUSE\source\repos\net 8.0 拦截器\\ConsoleAppNET8Test\Program.cs", line: 21, character: 9)]
//        public static void InterceptorPrint(this MyClass myclass, string s)
//        {
//            Console.WriteLine($"ABC.AOT下 MyClass.InterceptorPrint 拦截 MyClass.Print方法,参数是:{s}");
//        }
//    }
//}
#endregion

#region 调试3成功
//var c = new C();
//c.Interceptablemethod(1); // l1: prints "interceptor 1"
//c.Interceptablemethod(1); // l2: prints "other interceptor 1"
//c.Interceptablemethod(2); // l3: prints "other interceptor 2"
//c.Interceptablemethod(1); // prints "interceptable 1"
//public class C
//{
//    public void Interceptablemethod(int param)
//    {
//        Console.WriteLine($"interceptable {param}");
//    }
//}
//namespace ConsoleAppNET8Test
//{
//    public static class Interceptcuserservice
//    {
//        [InterceptsLocation(filePath: @"C:\Users\AUSE\source\repos\net 8.0 拦截器\\ConsoleAppNET8Test\Program.cs", line: 47, character: 3)]
//        public static void interceptormethod(this C c, int param)
//        {
//            Console.WriteLine($"interceptor {param}");
//        }
//    }
//}
#endregion

#region 调试4成功
namespace ConsoleAppNET8Test
{
    public static class InterceptUserService
    {
        [InterceptsLocation(filePath: @"C:\Users\AUSE\source\repos\net 8.0 拦截器\\ConsoleAppNET8Test\Program.cs", line: 90, character: 25)]
        public static void interceptormethod(this GetUserService example)
        {
            Console.WriteLine("Interceptor is here!");
        }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            GPG();
        }
        public static void GPG()
        {
            var userService = new GetUserService();
            userService.GetUserName();
            userService.AddUser();//替换掉
            userService.DeleteUser();
        }
    }
}
public class GetUserService
{
    public void GetUserName()
    {
        Console.WriteLine("GetUserName");
    }
    public void AddUser()
    {
        Console.WriteLine("AddUser");
    }

    public void DeleteUser()
    {
        Console.WriteLine("DeleteUser");
    }
}
#endregion
#region 公共部分
namespace System.Runtime.CompilerServices
{
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public sealed class InterceptsLocationAttribute(string filePath, int line, int character) : Attribute
    {
    }
}
#endregion
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值