����λ�ã���ҳ > �����̳� > �̳� > c# 12 ʵ������Interceptor ��ʲô��

c# 12 ʵ������Interceptor ��ʲô��

��Դ������������|��ʱ�䣺2024-08-07 09:48:28 |���Ķ���59��|�� ��ǩ�� T 2 һ�� C in ʵ�� inter �� |����������

һֱ�кܶ�ת��dotnet��Interceptor˵���ĵ��ģ�������˵��Interceptor���ʹ�õģ�����дһƪ��ʾ����չʾһ�� c# 12 ʵ������Interceptor ��ʲô�� �ٷ��������£���ʵ��˵�����������������õľ�̬��֯��ʽ��aop���ܣ���ͬ������il�޸Ĵ���ķ�ʽ��ʹ���ϵý�

��dotnet�����У���Ȼ�кܶ����Interceptor��ת��˵���ĵ����������й���Interceptor���ʹ�õ���ϸ˵�������Ľ�ͨ��һ����ʾ����չʾInterceptor��ʹ�÷�����

Interceptor��һ�����õľ�̬��֯��ʽ��aop���ܣ�������il�޸Ĵ���ķ�ʽ��ͬ���������ڱ���ʱ��������ʽ���Կ����ط����ĵ����滻Ϊ���������ĵ��á�ͨ�������������������ص��õ�Դλ�ã����Խ��������滻������������������У�������Դ�������У������´��룬�Ӷ��ṩ�������д������������������

��Դ��������ʹ���������޸����б���Ĵ��룬�������������Ӵ��롣Դ���������Կ����ط����ĵ����滻Ϊ�������������ĵ��á�

���������Ȥ�����������������Ķ����ܹ淶���˽���ϸ��Ϣ�����ʹ�øù��ܣ���ȷ����ʱ�˽��ʵ�鹦�ܵĹ��ܹ淶�е��κθ��ġ�����ȷ�����ܺ���΢���ĵ�վ�������Ӹ���ָ����

ʾ��

����������һ���򵥵� static method ��Ϊ���Ǹ�д�������ݵ�Ŀ�ꡣ

public static partial class DBExtensions
{
  public static string TestInterceptor(object o)
  {
      return o.GetType().ToString();
  }
}

�����ľ�̬���������Ǽ����д��Ŀ��Ϊ����o����������һ��string���͵�����ֵ��

����Ӧ�ÿ���ͨ�����µ� UT ������

[Fact]
public void CallNoError()
{
    Assert.Equal("sss", DBExtensions.TestInterceptor(new { A = "sss", C= "ddd" }));
}

���ʵ��

��һ�� �������

����һ�� netstandard2.0 ����Ⲣ�������£�


  
    netstandard2.0
    preview
    true
    
    false
    
  
  
    
    
  
  
    
    
  

�ڶ��� ���� UT ��Ŀ���� Interceptor ����

Generated Ŀ¼���ɴ����ļ���ʵ�ǷDZ���ģ�����Ϊ�˷����ҿ��� source generater ���ɵĴ����ļ����ݣ��������dz��γ���source generater���а�����


  
    net8.0
    enable
    enable
    false
    true
    Generated
    $(InterceptorsPreviewNamespaces);Test.AOT
  
  
    
  
  
    
  
  
    
  
  
    
    
  

������ ʵ�� InterceptorGenerator

[Generator(LanguageNames.CSharp)]
public class InterceptorGenerator : IIncrementalGenerator
{
}

����� IIncrementalGenerator Ϊsource generater��ǿ��Ƶ�һ���ӿڣ��и�ǿ�����ܺ͸����������������������ʵ�ֽӿڡ�

[Generator(LanguageNames.CSharp)]
public class InterceptorGenerator : IIncrementalGenerator
{
        public void Initialize(IncrementalGeneratorInitializationContext context)
        {
            var nodes = context.SyntaxProvider.CreateSyntaxProvider(FilterFunc, TransformFunc).Where(x => x is not null).Select((x, _) => x!);
            var combined = context.CompilationProvider.Combine(nodes.Collect());
            context.RegisterImplementationSourceOutput(combined, Generate);
        }
}

����������ʵ�� FilterFunc ��

private bool FilterFunc(SyntaxNode node, CancellationToken token)
{
    if (node is InvocationExpressionSyntax ie && ie.ChildNodes().FirstOrDefault() is MemberAccessExpressionSyntax ma)
    {
        return ma.Name.ToString().StartsWith("TestInterceptor");
    }
    return false;
}

����������ʵ�� TransformFunc ��

private TestData TransformFunc(GeneratorSyntaxContext ctx, CancellationToken token)
{
    // ʵ�ִ���
}

����������ʵ�� Generate ��

private void Generate(SourceProductionContext ctx, (Compilation Left, ImmutableArray Right) state)
{
    // ʵ�ִ���
}

���һ�� ��������

������DZ�����򣬾ͻῴ���������������ļ����롣


// ���ɵĴ���

�������ut�����Ҳ��ȷ��debug���е���Ҳ�ɿ����ϵ��ܽ����������ɵĴ����ļ��С�

С���Ƽ��Ķ�

�������������Ľ�Ϊ������Ϣ����������������ͬ���޹۵��֤ʵ��������

�����Ƶ����

����

ͬ������

����

ɨ��ά�����������ֻ��汾��

ɨ��ά����������΢�Ź��ںţ�

��վ�������������������ϴ��������ַ���İ�Ȩ���뷢�ʼ�[email protected]

��ICP��2022002427��-10 �湫��������43070202000427��© 2013~2025 haote.com ������