Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I love (and heavily use) source generators, but the development experience is godawful. Working with the raw Roslyn types is painful at best and this is compounded by them having to be written against .NET Standard, severely limiting the use of newer .NET functionality.

Eventually I want to write a good baseline library to use for my source generators -- simplifying finding definitions with attributes, mapping types to System.Type, adding some basic pattern matching for structures -- but haven't found a way to do it that's general enough while being very useful.



I agree, .NET Standard limitation unnecessarily complicates development experience. I think it's because some tools (Visual Studio) is still use legacy .NET Framework. I don't understand why they didn't integrate them via out of process architecture into these tools, since source generators didn't exist in the legacy framework anyway.

I sometimes generate code from plain CLI projects (avoiding source generators altogether), as whole debugging and DX is so much better.


Yeah, I went with that approach for most of the code generation in the emulator I'm currently working on. Source generators handle a few core things, but more advanced compilation tasks went to just ahead of time generation; couldn't get my parser combinator library to play nicely with .NET Standard, so that was just a dead-end.


You add PolySharp to your source generator project to get back some of the modern C# features. https://github.com/Sergio0694/PolySharp


.NET standard isn’t the biggest issue with making source generators. You can’t add dependencies to your project, which is an absolutely huge oversight IMO.


You can add dependencies, it's just important, that you also add the dependencies to the project where the source generator is used:

SourceGenerator.csproj:

  <ProjectReference Include="..\Dependency.csproj" />
FinalProject.csproj:

  <ProjectReference Include="..\Dependency.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  <ProjectReference Include="..\SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
And I also recommend to add "<LangVersion>latest</LangVersion>" to SourceGenerator.csproj to use new features like raw string literals.


Yeah, it is kind of sad that it was the community that had to step up for some T4 like experience instead of string concatenation.

That isn't as cool as Aspire and AI features.


I feel the exact same way. They can be insanely powerful, but the syntax is insanely off-putting.

And the documentation is still pretty sparse.

> Eventually I want to write a good baseline library to use for my source generators -- simplifying finding definitions with attributes, mapping types to System.Type, adding some basic pattern matching for structures -- but haven't found a way to do it that's general enough while being very useful.

I'd use it in a heartbeat. The new ForAttributeWithMetadataName function has been a big help, but everything else feels like a totally different language to me.


Add "<LangVersion>latest</LangVersion>" to the csproj to get more useful features like pattern matching and raw string literals.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: