Skip to content

Commit 64e2696

Browse files
committed
初始化器的动态依赖指向自身
1 parent 5c4dfba commit 64e2696

File tree

3 files changed

+12
-52
lines changed

3 files changed

+12
-52
lines changed

WebApiClientCore.Analyzers/HttpApiSourceGenerator.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void Initialize(GeneratorInitializationContext context)
2626
public void Execute(GeneratorExecutionContext context)
2727
{
2828
if (context.SyntaxReceiver is HttpApiSyntaxReceiver receiver)
29-
{
29+
{
3030
// System.Diagnostics.Debugger.Launch();
3131
var proxyClasses = receiver
3232
.GetHttpApiTypes(context.Compilation)
@@ -36,9 +36,7 @@ public void Execute(GeneratorExecutionContext context)
3636

3737
if (proxyClasses.Length > 0)
3838
{
39-
context.AddSource(HttpApiProxyClassStatic.FileName, HttpApiProxyClassStatic.ToSourceText());
4039
context.AddSource(HttpApiProxyClassInitializer.FileName, HttpApiProxyClassInitializer.ToSourceText());
41-
4240
foreach (var proxyClass in proxyClasses)
4341
{
4442
context.AddSource(proxyClass.FileName, proxyClass.ToSourceText());

WebApiClientCore.Analyzers/SourceGenerator/HttpApiProxyClass.Static.cs

Lines changed: 0 additions & 39 deletions
This file was deleted.

WebApiClientCore.Analyzers/SourceGenerator/HttpApiProxyClassInitializer.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
namespace WebApiClientCore.Analyzers.SourceGenerator
55
{
66
/// <summary>
7-
/// HttpApiProxyClass初始化器
7+
/// HttpApiProxyClass的静态类初始器
88
/// </summary>
99
static class HttpApiProxyClassInitializer
1010
{
1111
/// <summary>
1212
/// 文件名
1313
/// </summary>
14-
public static string FileName => $"{nameof(HttpApiProxyClassInitializer)}.g.cs";
14+
public static string FileName => $"{nameof(HttpApiProxyClass)}.g.cs";
1515

1616
/// <summary>
1717
/// 转换为SourceText
@@ -20,26 +20,27 @@ static class HttpApiProxyClassInitializer
2020
public static SourceText ToSourceText()
2121
{
2222
var code = $$"""
23-
#if NET5_0_OR_GREATER
2423
#pragma warning disable
24+
using System;
2525
namespace WebApiClientCore
2626
{
27-
/// <summary>动态依赖初始化器</summary>
28-
[global::System.Reflection.Obfuscation(Exclude = true)]
27+
/// <summary>HttpApi代理类</summary>
28+
[global::System.Reflection.Obfuscation(Exclude = true)]
2929
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
3030
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
31-
static partial class HttpApiProxyClassInitializer
32-
{
33-
/// <summary>初始化本程序集的动态依赖</summary>
31+
static partial class {{nameof(HttpApiProxyClass)}}
32+
{
33+
#if NET5_0_OR_GREATER
34+
/// <summary>初始化代理类</summary>
3435
[global::System.Runtime.CompilerServices.ModuleInitializer]
3536
[global::System.Diagnostics.CodeAnalysis.DynamicDependency(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All, typeof(global::WebApiClientCore.{{nameof(HttpApiProxyClass)}}))]
3637
public static void Initialize()
3738
{
3839
}
39-
}
40+
#endif
41+
}
4042
}
4143
#pragma warning restore
42-
#endif
4344
""";
4445
return SourceText.From(code, Encoding.UTF8);
4546
}

0 commit comments

Comments
 (0)