翼度科技»论坛 编程开发 .net 查看内容

学习Source Generators之IncrementalValueProvider

5

主题

5

帖子

15

积分

新手上路

Rank: 1

积分
15
前面我们使用了IIncrementalGenerator来生成代码,接下来我们来详细了解下IIncrementalGenerator的核心部分IncrementalValueProvider。
介绍

IncrementalValueProvider是基于管道的模式,将我们需要的数据进行处理转换后传递给SourceOutput。
目前官方提供可用的Providers有如下几种:

  • CompilationProvider
  • AdditionalTextsProvider
  • AnalyzerConfigOptionsProvider
  • MetadataReferencesProvider
  • ParseOptionsProvider
实操

接下来我们来使用AdditionalTextsProvider来学习IncrementalValueProvider的运行方式。
创建项目

首先创建LearnIncrementalValueProvider的控制台程序和LearnIncrementalValueProvider.Analysis的netstandard2.0类库两个项目。

按照前面HelloWorld项目的项目配置进行配置和引用。
添加LearnIncrementalValueProviderGenerator

在LearnIncrementalValueProvider.Analysis中添加LearnIncrementalValueProviderGenerator继承并实现IIncrementalGenerator接口。
  1. using Microsoft.CodeAnalysis;
  2. using System;
  3. using System.Diagnostics;
  4. namespace LearnIncrementalValueProvider.Analysis
  5. {
  6. <ItemGroup>
  7.   <AdditionalFiles Include="Files/*" />
  8. </ItemGroup><ItemGroup>
  9.   <AdditionalFiles Include="Files/*" />
  10. </ItemGroup>[Generator]
  11. <ItemGroup>
  12.   <AdditionalFiles Include="Files/*" />
  13. </ItemGroup><ItemGroup>
  14.   <AdditionalFiles Include="Files/*" />
  15. </ItemGroup>public class LearnIncrementalValueProviderGenerator : IIncrementalGenerator
  16. <ItemGroup>
  17.   <AdditionalFiles Include="Files/*" />
  18. </ItemGroup><ItemGroup>
  19.   <AdditionalFiles Include="Files/*" />
  20. </ItemGroup>{
  21. <ItemGroup>
  22.   <AdditionalFiles Include="Files/*" />
  23. </ItemGroup><ItemGroup>
  24.   <AdditionalFiles Include="Files/*" />
  25. </ItemGroup><ItemGroup>
  26.   <AdditionalFiles Include="Files/*" />
  27. </ItemGroup><ItemGroup>
  28.   <AdditionalFiles Include="Files/*" />
  29. </ItemGroup>public void Initialize(IncrementalGeneratorInitializationContext context)
  30. <ItemGroup>
  31.   <AdditionalFiles Include="Files/*" />
  32. </ItemGroup><ItemGroup>
  33.   <AdditionalFiles Include="Files/*" />
  34. </ItemGroup><ItemGroup>
  35.   <AdditionalFiles Include="Files/*" />
  36. </ItemGroup><ItemGroup>
  37.   <AdditionalFiles Include="Files/*" />
  38. </ItemGroup>{
  39. <ItemGroup>
  40.   <AdditionalFiles Include="Files/*" />
  41. </ItemGroup><ItemGroup>
  42.   <AdditionalFiles Include="Files/*" />
  43. </ItemGroup><ItemGroup>
  44.   <AdditionalFiles Include="Files/*" />
  45. </ItemGroup><ItemGroup>
  46.   <AdditionalFiles Include="Files/*" />
  47. </ItemGroup><ItemGroup>
  48.   <AdditionalFiles Include="Files/*" />
  49. </ItemGroup><ItemGroup>
  50.   <AdditionalFiles Include="Files/*" />
  51. </ItemGroup>Debugger.Launch();
  52. <ItemGroup>
  53.   <AdditionalFiles Include="Files/*" />
  54. </ItemGroup><ItemGroup>
  55.   <AdditionalFiles Include="Files/*" />
  56. </ItemGroup><ItemGroup>
  57.   <AdditionalFiles Include="Files/*" />
  58. </ItemGroup><ItemGroup>
  59.   <AdditionalFiles Include="Files/*" />
  60. </ItemGroup><ItemGroup>
  61.   <AdditionalFiles Include="Files/*" />
  62. </ItemGroup><ItemGroup>
  63.   <AdditionalFiles Include="Files/*" />
  64. </ItemGroup>var additionalTextsProvider = context.AdditionalTextsProvider;
  65. <ItemGroup>
  66.   <AdditionalFiles Include="Files/*" />
  67. </ItemGroup><ItemGroup>
  68.   <AdditionalFiles Include="Files/*" />
  69. </ItemGroup><ItemGroup>
  70.   <AdditionalFiles Include="Files/*" />
  71. </ItemGroup><ItemGroup>
  72.   <AdditionalFiles Include="Files/*" />
  73. </ItemGroup><ItemGroup>
  74.   <AdditionalFiles Include="Files/*" />
  75. </ItemGroup><ItemGroup>
  76.   <AdditionalFiles Include="Files/*" />
  77. </ItemGroup>context.RegisterSourceOutput(additionalTextsProvider, (ctx, additionalTexts) =>
  78. <ItemGroup>
  79.   <AdditionalFiles Include="Files/*" />
  80. </ItemGroup><ItemGroup>
  81.   <AdditionalFiles Include="Files/*" />
  82. </ItemGroup><ItemGroup>
  83.   <AdditionalFiles Include="Files/*" />
  84. </ItemGroup><ItemGroup>
  85.   <AdditionalFiles Include="Files/*" />
  86. </ItemGroup><ItemGroup>
  87.   <AdditionalFiles Include="Files/*" />
  88. </ItemGroup><ItemGroup>
  89.   <AdditionalFiles Include="Files/*" />
  90. </ItemGroup><ItemGroup>
  91.   <AdditionalFiles Include="Files/*" />
  92. </ItemGroup><ItemGroup>
  93.   <AdditionalFiles Include="Files/*" />
  94. </ItemGroup><ItemGroup>
  95.   <AdditionalFiles Include="Files/*" />
  96. </ItemGroup><ItemGroup>
  97.   <AdditionalFiles Include="Files/*" />
  98. </ItemGroup><ItemGroup>
  99.   <AdditionalFiles Include="Files/*" />
  100. </ItemGroup><ItemGroup>
  101.   <AdditionalFiles Include="Files/*" />
  102. </ItemGroup><ItemGroup>
  103.   <AdditionalFiles Include="Files/*" />
  104. </ItemGroup><ItemGroup>
  105.   <AdditionalFiles Include="Files/*" />
  106. </ItemGroup><ItemGroup>
  107.   <AdditionalFiles Include="Files/*" />
  108. </ItemGroup><ItemGroup>
  109.   <AdditionalFiles Include="Files/*" />
  110. </ItemGroup><ItemGroup>
  111.   <AdditionalFiles Include="Files/*" />
  112. </ItemGroup><ItemGroup>
  113.   <AdditionalFiles Include="Files/*" />
  114. </ItemGroup><ItemGroup>
  115.   <AdditionalFiles Include="Files/*" />
  116. </ItemGroup><ItemGroup>
  117.   <AdditionalFiles Include="Files/*" />
  118. </ItemGroup> {
  119. <ItemGroup>
  120.   <AdditionalFiles Include="Files/*" />
  121. </ItemGroup><ItemGroup>
  122.   <AdditionalFiles Include="Files/*" />
  123. </ItemGroup><ItemGroup>
  124.   <AdditionalFiles Include="Files/*" />
  125. </ItemGroup><ItemGroup>
  126.   <AdditionalFiles Include="Files/*" />
  127. </ItemGroup><ItemGroup>
  128.   <AdditionalFiles Include="Files/*" />
  129. </ItemGroup><ItemGroup>
  130.   <AdditionalFiles Include="Files/*" />
  131. </ItemGroup><ItemGroup>
  132.   <AdditionalFiles Include="Files/*" />
  133. </ItemGroup><ItemGroup>
  134.   <AdditionalFiles Include="Files/*" />
  135. </ItemGroup><ItemGroup>
  136.   <AdditionalFiles Include="Files/*" />
  137. </ItemGroup><ItemGroup>
  138.   <AdditionalFiles Include="Files/*" />
  139. </ItemGroup><ItemGroup>
  140.   <AdditionalFiles Include="Files/*" />
  141. </ItemGroup><ItemGroup>
  142.   <AdditionalFiles Include="Files/*" />
  143. </ItemGroup><ItemGroup>
  144.   <AdditionalFiles Include="Files/*" />
  145. </ItemGroup><ItemGroup>
  146.   <AdditionalFiles Include="Files/*" />
  147. </ItemGroup><ItemGroup>
  148.   <AdditionalFiles Include="Files/*" />
  149. </ItemGroup><ItemGroup>
  150.   <AdditionalFiles Include="Files/*" />
  151. </ItemGroup><ItemGroup>
  152.   <AdditionalFiles Include="Files/*" />
  153. </ItemGroup><ItemGroup>
  154.   <AdditionalFiles Include="Files/*" />
  155. </ItemGroup><ItemGroup>
  156.   <AdditionalFiles Include="Files/*" />
  157. </ItemGroup><ItemGroup>
  158.   <AdditionalFiles Include="Files/*" />
  159. </ItemGroup><ItemGroup>
  160.   <AdditionalFiles Include="Files/*" />
  161. </ItemGroup><ItemGroup>
  162.   <AdditionalFiles Include="Files/*" />
  163. </ItemGroup> var path = additionalTexts.Path;
  164. <ItemGroup>
  165.   <AdditionalFiles Include="Files/*" />
  166. </ItemGroup><ItemGroup>
  167.   <AdditionalFiles Include="Files/*" />
  168. </ItemGroup><ItemGroup>
  169.   <AdditionalFiles Include="Files/*" />
  170. </ItemGroup><ItemGroup>
  171.   <AdditionalFiles Include="Files/*" />
  172. </ItemGroup><ItemGroup>
  173.   <AdditionalFiles Include="Files/*" />
  174. </ItemGroup><ItemGroup>
  175.   <AdditionalFiles Include="Files/*" />
  176. </ItemGroup><ItemGroup>
  177.   <AdditionalFiles Include="Files/*" />
  178. </ItemGroup><ItemGroup>
  179.   <AdditionalFiles Include="Files/*" />
  180. </ItemGroup><ItemGroup>
  181.   <AdditionalFiles Include="Files/*" />
  182. </ItemGroup><ItemGroup>
  183.   <AdditionalFiles Include="Files/*" />
  184. </ItemGroup><ItemGroup>
  185.   <AdditionalFiles Include="Files/*" />
  186. </ItemGroup><ItemGroup>
  187.   <AdditionalFiles Include="Files/*" />
  188. </ItemGroup><ItemGroup>
  189.   <AdditionalFiles Include="Files/*" />
  190. </ItemGroup><ItemGroup>
  191.   <AdditionalFiles Include="Files/*" />
  192. </ItemGroup><ItemGroup>
  193.   <AdditionalFiles Include="Files/*" />
  194. </ItemGroup><ItemGroup>
  195.   <AdditionalFiles Include="Files/*" />
  196. </ItemGroup><ItemGroup>
  197.   <AdditionalFiles Include="Files/*" />
  198. </ItemGroup><ItemGroup>
  199.   <AdditionalFiles Include="Files/*" />
  200. </ItemGroup><ItemGroup>
  201.   <AdditionalFiles Include="Files/*" />
  202. </ItemGroup><ItemGroup>
  203.   <AdditionalFiles Include="Files/*" />
  204. </ItemGroup><ItemGroup>
  205.   <AdditionalFiles Include="Files/*" />
  206. </ItemGroup><ItemGroup>
  207.   <AdditionalFiles Include="Files/*" />
  208. </ItemGroup> var text = additionalTexts.GetText(ctx.CancellationToken);
  209. <ItemGroup>
  210.   <AdditionalFiles Include="Files/*" />
  211. </ItemGroup><ItemGroup>
  212.   <AdditionalFiles Include="Files/*" />
  213. </ItemGroup><ItemGroup>
  214.   <AdditionalFiles Include="Files/*" />
  215. </ItemGroup><ItemGroup>
  216.   <AdditionalFiles Include="Files/*" />
  217. </ItemGroup><ItemGroup>
  218.   <AdditionalFiles Include="Files/*" />
  219. </ItemGroup><ItemGroup>
  220.   <AdditionalFiles Include="Files/*" />
  221. </ItemGroup><ItemGroup>
  222.   <AdditionalFiles Include="Files/*" />
  223. </ItemGroup><ItemGroup>
  224.   <AdditionalFiles Include="Files/*" />
  225. </ItemGroup><ItemGroup>
  226.   <AdditionalFiles Include="Files/*" />
  227. </ItemGroup><ItemGroup>
  228.   <AdditionalFiles Include="Files/*" />
  229. </ItemGroup><ItemGroup>
  230.   <AdditionalFiles Include="Files/*" />
  231. </ItemGroup><ItemGroup>
  232.   <AdditionalFiles Include="Files/*" />
  233. </ItemGroup><ItemGroup>
  234.   <AdditionalFiles Include="Files/*" />
  235. </ItemGroup><ItemGroup>
  236.   <AdditionalFiles Include="Files/*" />
  237. </ItemGroup><ItemGroup>
  238.   <AdditionalFiles Include="Files/*" />
  239. </ItemGroup><ItemGroup>
  240.   <AdditionalFiles Include="Files/*" />
  241. </ItemGroup><ItemGroup>
  242.   <AdditionalFiles Include="Files/*" />
  243. </ItemGroup><ItemGroup>
  244.   <AdditionalFiles Include="Files/*" />
  245. </ItemGroup><ItemGroup>
  246.   <AdditionalFiles Include="Files/*" />
  247. </ItemGroup><ItemGroup>
  248.   <AdditionalFiles Include="Files/*" />
  249. </ItemGroup> });
  250. <ItemGroup>
  251.   <AdditionalFiles Include="Files/*" />
  252. </ItemGroup><ItemGroup>
  253.   <AdditionalFiles Include="Files/*" />
  254. </ItemGroup><ItemGroup>
  255.   <AdditionalFiles Include="Files/*" />
  256. </ItemGroup><ItemGroup>
  257.   <AdditionalFiles Include="Files/*" />
  258. </ItemGroup>}
  259. <ItemGroup>
  260.   <AdditionalFiles Include="Files/*" />
  261. </ItemGroup><ItemGroup>
  262.   <AdditionalFiles Include="Files/*" />
  263. </ItemGroup>}
  264. }
复制代码
在实现的代码中,获取到AdditionalTextsProvider,并直接传递给RegisterSourceOutput,并在委托方法中直接获取AdditionalTextsProvider的文件路径以及文本内容。
在方法中加入Debugger.Launch();方便调试。
添加文件和调试

在控制台程序中,添加一个Files目录。往里面塞入一个swagger.json文件。
此时直接调试会发现,断点并不会进入到RegisterSourceOutput的委托中。

这是因为AdditionalTextsProvider并没有找到任何需要加载的文件。
我们需要在控制台程序的项目文件中添加AdditionalFiles,指定需要监听的文件。
  1. <ItemGroup>
  2.   <AdditionalFiles Include="Files/*" />
  3. </ItemGroup>
复制代码
添加AdditionalFiles后,在调试一次。

可以看到断点成功进来了。并且可以看到获取的文件路径以及文件的文本内容。

多个文件

在Files目录中添加一个txt文件。并写入文本HelloWorld

然后再调试一次。可以发现,每一个文件都会单独执行一次委托的方法。

过滤文件

当我们只需要其中一种类型的文件的时候,我们可以通过Where来进行过滤筛选。

通过Debugger.Log可以发现,只输出了json的文件路径。

处理数据

可以使用Select来处理我们的数据,比如这里我只获取文件名称。通过Debugger.Log可以看到输出了两个文件名称。

集合

如果不想多次处理文件的话,可以使用Collect方法,直接把多个文件合并在一起。

这里可以看到,使用Collect,2个文件可以同时处理。
组合多个IncrementalValueProvider

除了对单个IncrementalValueProvider进行处理外,我们还可以组合不同的IncrementalValueProvider。
比如将CompilationProvider和AdditionalTextsProvider组合起来。
使用Combine方法。

可以看到 paris的Right和Left分别是CompilationProvider和AdditionalTextsProvider两种类型。
结语

以上就是IncrementalValueProvider比较常用的方式。通过这些操作可以灵活的实现我们的代码生成逻辑。
当然还有其他的IncrementalValueProvider,这里就不都写出来了。其他的可以自己实操玩起来~
本文代码仓库地址https://github.com/fanslead/Learn-SourceGenerator

来源:https://www.cnblogs.com/fanshaoO/p/18108051
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具