3

I try to use .NET 5 Source Generators for generate webapi controllers starting from commands and queries.

I use this article [example]https://www.edument.se/en/blog/post/net-5-source-generators-mediatr-cqrs for this

But i want use commands and queries classes in external project referenced assembly for generate controllers in webapi project but I don't understand how give instructions to syntax receiver for this.

The structure of my solution is:

Projects

SourceGenerator.csproj

   `MySourceGenerator.cs`

Application.csproj

 Commands
     `ExampleCommand.cs`

WebApi.csproj

   References
      `SourceGenerator.csproj` (with OutputItemType="Analyzer")
      `Application.csproj`
   Controllers
      `VirtualExpectedGeneratedController.cs`
ucollina
  • 31
  • 3
  • If the compiler does not compile these classes then you will not get them in the syntax tree. – Ackdari Feb 03 '21 at 09:11
  • See https://github.com/dotnet/roslyn/discussions/47517 . Some bits of it are about ProjectReference, but other bits are about PackageReference – canton7 Feb 03 '21 at 09:12
  • But what you could try is to create a class that simply holds one private static Property for all the query and command classes you want and then work with this information in the source generator – Ackdari Feb 03 '21 at 09:12
  • See also [this example](https://github.com/dotnet/roslyn-sdk/blob/0313c80ed950ac4f4eef11bb2e1c6d1009b328c4/samples/CSharp/SourceGenerators/SourceGeneratorSamples/SourceGeneratorSamples.csproj#L13-L30) linked from the discussion above – canton7 Feb 03 '21 at 09:13
  • but if i have in my solution a project in .net5 how I can reference this? can I add only standard project? – ucollina Feb 03 '21 at 09:48
  • See the linked https://github.com/dotnet/roslyn/issues/47087 . If you want your source generator to run with msbuild and Visual Studio, it needs to target netstandard2.0. Visual Studio and msbuild run on .NET Framework. – canton7 Feb 03 '21 at 10:11
  • _"but if i have in my solution a project in .net5 how I can reference this? can I add only standard project?"_ please extend the question with information about: Solution-Structure, which project is the generator, dependency graph/list of the projects, which projects holds the classes that you are interested in using – Ackdari Feb 03 '21 at 11:53
  • @Ackdari I've added references in the web project to both, Generator and Application project. But I don't know how to make the SourceGenerator class to get the classes in the Application project – Wahid Bitar Feb 10 '22 at 17:12

0 Answers0