在网上集各家之所长的基础上写的,大部分在其他文章里都能找到,方便自己看
如果写拦截器的的时候报签名的错误,多是参数不匹配,就是下图框起来的地方问题
配置文件.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