walterlv / BlogComments

3 stars 0 forks source link

post/generate-csharp-source-using-roslyn-source-generator #114

Open utterances-bot opened 2 years ago

utterances-bot commented 2 years ago

使用 Source Generator 在编译你的 .NET 项目时自动生成代码 - walterlv

本文将带你为你的某个库添加自动生成代码的逻辑。 本文以 dotnetCampus.Ipc 项目为例,来说明如何为一个现成的 .NET 类库添加自动生成代码的功能。这是一个在本机内进行进程间通信的库,在你拥有一个 IPC 接口和对应的实现之后,本库还会自动帮你生成通过 IPC 代理访问的代码。由于项目加了 Roslyn

https://blog.walterlv.com/post/generate-csharp-source-using-roslyn-source-generator

YHLShaw commented 2 years ago

Source Generator 有点 yyds 的意思

YHLShaw commented 2 years ago

配合 Roslyn 的 语义分析 让编译器 来生成代码,简直比那些用工具生成的甩好几条街

walterlv commented 2 years ago

@YHLShaw 对,同感!

operhero commented 1 year ago

我参照https://blog.walterlv.com/post/develop-a-code-analyzer-for-both-nuget-and-visual-studio-extension.html#%E5%AE%89%E8%A3%85-visual-studio-%E6%89%A9%E5%B1%95%E5%BC%80%E5%8F%91%E5%B7%A5%E4%BD%9C%E8%B4%9F%E8%BD%BD写了个分析器,然后结合这篇文章,遇到了一个问题: 无法从 D:\test\Test2\Analyzer\bin\Debug\Analyzer.dll 创建分析器 Analyzer.CatchAnalyzer 的实例: Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. 系统找不到指定的文件。。
大神能帮忙看看吗,这个问题不知道从哪看起

operhero commented 1 year ago

使用dotnet core sdk6.0 错误消失 使用sdk5.0就报错 分析器的两个依赖库为

  <PackageReference Include="NETStandard.Library" Version="2.0.3" PrivateAssets="all" />
operhero commented 1 year ago

"Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.0.1" PrivateAssets="all" "NETStandard.Library" Version="2.0.3" PrivateAssets="all"

operhero commented 1 year ago

我把有问题的工程升到6然后回退到5,又没问题了。。

operhero commented 1 year ago

对已经存在的工程添加分析工程,就会报这个错。修改一下global.json中sdk.version就好了。重新rebuild、restore都不起作用。不知道除了修改sdk版本,还有其他办法让后添加的分析工程生效吗?

operhero commented 1 year ago

上边说错了,回退到dotnet core 5.0问题依旧在。看来是sdk版本的问题

operhero commented 1 year ago

适合dotnet core5.0 的Microsoft.CodeAnalysis.CSharp.Workspaces最大版本为3.9.0 我自己试出来的,不清楚官网有没有说明