第一个吃螃蟹的落地项目#
一直比较关心SourceGenerator,现在我觉得,SourceGenerator现在已到达可以使用的阶段了。WebApiClientCore之前有个分支做SourceGenerator的实验,但迟迟没有合并到master来。现在它已经合并到master,并以一个Extensions.SourceGenerator扩展包的方式出现,让WebApiClientCore多一种代理类生成的方式选择。这个扩展包编写时非常简单,我已经不想看以前是怎么用Cecil为程序集插入静态IL的代码了。

4 如何编写xxxSourceGenerator#
创建一个netstandard2.0的程序集#
Copy

<PropertyGroup><TargetFramework>netstandard2.0</TargetFramework><LangVersion>8.0</LangVersion><Nullable>enable</Nullable>
</PropertyGroup><ItemGroup><PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" PrivateAssets="all" /><PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2" PrivateAssets="all" />
</ItemGroup>

实现ISyntaxReceiver,接收编译时语法树的遍历# Copy class xxxSyntaxReceiver : ISyntaxReceiver { /// /// xxx感兴趣的接口列表 /// private readonly List interfaceSyntaxList = new List();

/// <summary>
/// 访问语法树
/// </summary>
/// <param name="syntaxNode"></param>
void ISyntaxReceiver.OnVisitSyntaxNode(SyntaxNode syntaxNode)
{if (syntaxNode is InterfaceDeclarationSyntax syntax){this.interfaceSyntaxList.Add(syntax);}
}

}
实现ISourceGenerator,且使用[Generator]特性#
Copy
[Generator]
public class xxxSourceGenerator : ISourceGenerator
{
///
/// 初始化
///
///
public void Initialize(GeneratorInitializationContext context)
{
context.RegisterForSyntaxNotifications(() => new xxxSyntaxReceiver());
}

/// <summary>
/// 执行
/// </summary>
/// <param name="context"></param>
public void Execute(GeneratorExecutionContext context)
{if (context.SyntaxReceiver is xxxSyntaxReceiver receiver){// 从receiver获取你感兴趣的语法节点// 然后拼接成string的代码// 把代码添加到contextcontext.AddSource("代码1的id","这里是c#代码,会参与编译的");}
}

}
5 如何调试xxxSourceGenerator#
在被调试项目以分析器方式引入xxxSourceGenerator项目#
Copy

在xxxSourceGenerator里加入Debugger.Launch()#
没错,这是最简单的触发调试方式,你在xxxSourceGenerator入口加这么一行代码,被调试的项目只要一编译,vs就弹出且断点到Debugger.Launch()这行,然后就可以一步一步执行调试了。

6 如何打包发布xxxSourceGenerator#
SourceGenerator项目本质上还是分析器项目,所以可以打包成一个nuget包,别的项目引用这个nuget包之后,就自动以分析器的方式安装到目标项目中,然后激活了你的xxxSourceGenerator。

分析器的nuget打包#
需要将编译出的xxxSourceGenerator.dll放到nuget包的analyzers\dotnet\cs目录下
需要在nuget包的tools目录下放置分析器安装和卸载脚本install.ps1和uninstall.ps1,这脚本是通用的。
install.ps1#
Copy
param($installPath, $toolsPath, $package, $project)

$analyzersPaths = Join-Path (Join-Path (Split-Path -Path $toolsPath -Parent) “analyzers” ) * -Resolve

foreach($analyzersPath in $analyzersPaths)
{
# Install the language agnostic analyzers.
if (Test-Path KaTeX parse error: Expected '}', got 'EOF' at end of input: … foreach (analyzerFilePath in Get-ChildItem KaTeX parse error: Expected '}', got 'EOF' at end of input: … if(project.Object.AnalyzerReferences)
{
project.Object.AnalyzerReferences.Add(project.Object.AnalyzerReferences.Add(project.Object.AnalyzerReferences.Add(analyzerFilePath.FullName)
}
}
}
}

$project.Type gives the language name like (C# or VB.NET)

languageFolder=""if(languageFolder = "" if(languageFolder=""if(project.Type -eq “C#”)
{
KaTeX parse error: Expected 'EOF', got '}' at position 23: …eFolder = "cs" }̲ if(project.Type -eq “VB.NET”)
{
KaTeX parse error: Expected 'EOF', got '}' at position 23: …eFolder = "vb" }̲ if(languageFolder -eq “”)
{
return
}

foreach($analyzersPath in $analyzersPaths)
{
# Install language specific analyzers.
$languageAnalyzersPath = join-path $analyzersPath $languageFolder
if (Test-Path KaTeX parse error: Expected '}', got 'EOF' at end of input: … foreach (analyzerFilePath in Get-ChildItem KaTeX parse error: Expected '}', got 'EOF' at end of input: … if(project.Object.AnalyzerReferences)
{
project.Object.AnalyzerReferences.Add(project.Object.AnalyzerReferences.Add(project.Object.AnalyzerReferences.Add(analyzerFilePath.FullName)
}
}
}
}
uninstall.ps1#
Copy
param($installPath, $toolsPath, $package, $project)

$analyzersPaths = Join-Path (Join-Path (Split-Path -Path $toolsPath -Parent) “analyzers” ) * -Resolve

foreach($analyzersPath in $analyzersPaths)
{
# Uninstall the language agnostic analyzers.
if (Test-Path KaTeX parse error: Expected '}', got 'EOF' at end of input: … foreach (analyzerFilePath in Get-ChildItem KaTeX parse error: Expected '}', got 'EOF' at end of input: … if(project.Object.AnalyzerReferences)
{
project.Object.AnalyzerReferences.Remove(project.Object.AnalyzerReferences.Remove(project.Object.AnalyzerReferences.Remove(analyzerFilePath.FullName)
}
}
}
}

$project.Type gives the language name like (C# or VB.NET)

languageFolder=""if(languageFolder = "" if(languageFolder=""if(project.Type -eq “C#”)
{
KaTeX parse error: Expected 'EOF', got '}' at position 23: …eFolder = "cs" }̲ if(project.Type -eq “VB.NET”)
{
KaTeX parse error: Expected 'EOF', got '}' at position 23: …eFolder = "vb" }̲ if(languageFolder -eq “”)
{
return
}

foreach($analyzersPath in $analyzersPaths)
{
# Uninstall language specific analyzers.
$languageAnalyzersPath = join-path $analyzersPath $languageFolder
if (Test-Path KaTeX parse error: Expected '}', got 'EOF' at end of input: … foreach (analyzerFilePath in Get-ChildItem KaTeX parse error: Expected '}', got 'EOF' at end of input: … if(project.Object.AnalyzerReferences)
{
try
{
project.Object.AnalyzerReferences.Remove(project.Object.AnalyzerReferences.Remove(project.Object.AnalyzerReferences.Remove(analyzerFilePath.FullName)
}
catch
{

            }}}
}

}
USB Microphone https://www.soft-voice.com/
Wooden Speakers https://www.zeshuiplatform.com/
亚马逊测评 www.yisuping.cn
深圳网站建设www.sz886.com

SourceGenerator入门指北相关推荐

  1. Python 简单入门指北(二)

    Python 简单入门指北(二) 2 函数 2.1 函数是一等公民 一等公民指的是 Python 的函数能够动态创建,能赋值给别的变量,能作为参传给函数,也能作为函数的返回值.总而言之,函数和普通变量 ...

  2. Flutter 入门指北(Part 9)之弹窗和提示(SnackBar、BottomSheet、Dialog)

    该文已授权公众号 「码个蛋」,转载请指明出处 前面的小节把常用的一些部件都介绍了,这节介绍下 Flutter 中的一些操作提示.Flutter 中的操作提示主要有这么几种 SnackBar.Botto ...

  3. 计算机学习入门指北——计科软工网络信安侧重图析、解读专业术语、岗位分类、未来规划

    申明:本博文偏技术向,主观性较强,其中部分理解必有偏差和误解,望指出改正! 计算机学习入门指北: 作为刚入学的计算机系学生,面对一片专业术语十分蒙.区块链?大数据?开源?数据库?嵌入式开发?前端后端? ...

  4. 【杭电数电实验】verilog入门指北

    verilog入门指北 前言 指北内容 面向人群 基础实验 1-15 代码参考 正文 ISE 的安装 实验的基本操作流程 可能出现的问题 创建工程闪退 希望删除某一文件,实际上并没有删除 如何编写测试 ...

  5. 萌妹子Python入门指北(二)

    原文来自 (ixindoo.com)[http://ixindoo.com/articles/662] 只写了第一篇就好久没更新了,为啥?因为妹子学编程的意愿不强了,我也不能逼迫她去学.不过后来收到部 ...

  6. 【Linux入门指北】第一篇 初识Linux

    目录 前言 一.Linux操作系统的发展历史 1.Linux操作系统的诞生 2.Linux操作系统的发展 1.自由软件基金会(FSF) 2.GPL协议 3.GUN工程 二.Linux的不同发行版本 1 ...

  7. Python 简单入门指北(试读版)

    本文是我小专栏中 Python 简单入门指北 一文的前半部分,如果你能坚持读完并且觉得有一定收获,建议阅读原文,只需一杯咖啡钱就可以阅读更精彩的部分,也可以订阅小专栏或者加入我的知识星球,价格都是 6 ...

  8. Android环信3.0即时通讯云入门指北

    Android环信3.0即时通讯云入门指北 官方文档 http://docs-im.easemob.com/im/android/sdk/import 基础集成 http://docs-im.ease ...

  9. unity hub版本管理工具 中文包 及入门指北

    2021/12/19更新 目录 一.环境 二.hub装unity 三.中文包 四.一些事情 五.意外情况 六.hub和unity下载的一些坑 七.入门指北 一.环境 内网环境 win (其它不管) 二 ...

  10. TensorRT详细入门指北,如果你还不了解TensorRT,过来看看吧

    首发于TensorRT详细入门指北,如果你还不了解TensorRT,过来看看吧!,最新回复以及交流请看这里~ 推荐一个深蓝学院的CUDA课程,TensorRT_Tutorial的作者伟哥讲解的,质量很 ...

最新文章

  1. MySQL Cluster(MySQL 集群) 初试(转)
  2. Centos 6.0/ Nginx 安装与配置
  3. 使用.NET Core创建Windows服务 - 使用.NET Core工作器方式
  4. 腾讯视频怎么设置下载视频位置
  5. 10.08-vscode-plantuml建模
  6. 第一行代码 Hello world
  7. html开源flash视频播放器代码下载
  8. 谈谈自己对移动互联网的看法
  9. discuz template 模板文件说明
  10. The server is invalid. Error occurred reading server credential. Required file or directory ‘serverO
  11. 服务器 *.s3.amazonaws.com上资源不可用的处理方式
  12. 学习Java可以从事哪些岗位?
  13. 小破练习-嵌套循环及列表
  14. 我是一个下「笨功夫」的人
  15. 土地利用覆被变化的概念_土地利用/覆被变化(LUCC)研究现状与展望
  16. C++ 代码模拟登录淘宝、天猫、支付宝等电商网站的实现
  17. 怎么实现边下载边播放
  18. SQL 语句耗时查询
  19. 量子计算机天使粒子张首晟,杨振宁弟子张首晟:发现“天使粒子” 证明2=0
  20. Go语言自学系列 | golang递归

热门文章

  1. 租房申请html模板,实用的租房房源标题模板
  2. Golang 内建类型和内建函数 builtin包 注释翻译
  3. 大数据SQL中的Join【谓词下推】讲解
  4. matlab 数字显示完整,如何在matlab中同时显示字符串和数字?
  5. win10定时锁定计算机,Win10自动锁屏时间太长了怎么设置?Win10设置自动锁屏时间的操作...
  6. 100条历史典故,读懂半个中国
  7. MySQL的事务特性
  8. Carry On My Wayward Son -- Kansas
  9. .net开通exchange邮箱
  10. 英文信详解:申请信、推荐信等等