.NET SourceGenerator API接口生成实现类
Posted EdisonTalk
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.NET SourceGenerator API接口生成实现类相关的知识,希望对你有一定的参考价值。
Identity a Interface which will be implemented by SourceGenerators: HttpClient name
Name Create a new
Create a new with given name
name)
Name = name;
Identity a method send HTTP Get request
: Creates a new with the given route template.
template) :
HTTP method abstract type for common encapsulation
: Route template
Template
Creates a new with the given route template.
template)
Template = template;
...
/>
/>
/>
...
: List<INamedTypeSymbol> TypeSymbols List<INamedTypeSymbol>();
(context.Node InterfaceDeclarationSyntax ids && ids.AttributeLists.Count >
typeSymbol = ModelExtensions.GetDeclaredSymbol(context.SemanticModel, ids) INamedTypeSymbol;
(typeSymbol!.GetAttributes().Any(x =>
x.AttributeClass!.ToDisplayString() ==
TypeSymbols.Add(typeSymbol);
httpClientName,
requestUri)
returnType = (methodSymbol.ReturnType INamedTypeSymbol).TypeArguments[ cancellationToken = methodSymbol.Parameters.Last().Name;
source = GenerateHttpClient(typeSymbol, methodSymbol, httpClientName);
source.AppendLine( source.AppendLine( source.AppendLine(
source.AppendLine( source.ToString();
extensionSource = StringBuilder( public static class ScanInjectOptions
public static void AddGeneratedHttpClient(this IServiceCollection services)
(typeSymbol receiver.TypeSymbols)
...
extensionSource.AppendLine(
extensionSource.AppendLine( extensionTextFormatted = CSharpSyntaxTree
.ParseText(extensionSource.ToString(), CSharpParseOptions(LanguageVersion.CSharp8)).GetRoot()
.NormalizeWhitespace().SyntaxTree.GetText().ToString();
context.AddSource( SourceText.From(extensionTextFormatted, Encoding.UTF8));
: HashSet<INamedTypeSymbol> _httpClientTypeSymbols;
_httpClientTypeSymbols = HashSet<INamedTypeSymbol>(SymbolEqualityComparer.Default);
ImmutableArray<INamedTypeSymbol> => _httpClientTypeSymbols.ToImmutableArray();
symbol.GlobalNamespace.Accept(
(namespaceOrTypeSymbol symbol.GetMembers())
namespaceOrTypeSymbol.Accept(
(symbol.DeclaredAccessibility != Accessibility.Public)
(symbol.GetAttributes().Any(x =>
x.AttributeClass!.ToDisplayString() ==
_httpClientTypeSymbols.Add(symbol);
nestedTypes = symbol.GetMembers();
(nestedTypes.IsDefaultOrEmpty)
(nestedType nestedTypes)
nestedType.Accept(
(!(context.SyntaxContextReceiver HttpClientSyntax receiver))
httpClientVisitor = HttpClientVisitor();
(assemblySymbol context.Compilation.SourceModule.ReferencedAssemblySymbols
.Where(x => x.Identity.PublicKey == ImmutableArray<
assemblySymbol.Accept(httpClientVisitor);
receiver.TypeSymbols.AddRange(httpClientVisitor.GetHttpClientTypes());
...
[ id, CancellationToken cancellationToken = [ [ [ id, Todo todo, CancellationToken cancellationToken);
[ id, CancellationToken cancellationToken);
IJsonServerApi _jsonServerApi;
_jsonServerApi = jsonServerApi;
[ Task<Todo> id, CancellationToken cancellationToken)
_jsonServerApi.Get(id, cancellationToken);
...
SourceCode: https://github.com/huiyuanai709/SourceGeneratorPower
Nuget Package: https://www.nuget.org/packages/SourceGeneratorPower.HttpClient.Abstractions
Nuget Package: https://www.nuget.org/packages/SourceGeneratorPower.HttpClient.SourceGenerator