The classic form is required because this first source generator writes a partial method in that Program class: namespace ConsoleApp The following code doesn't use top level statements. Replace the Program class with the following code. ![]() In this guide, you'll explore the creation of a source generator using the ISourceGenerator API.Ĭreate a. This would reduce the number of times a mistyped string literal leads to a request not hitting the correct controller. With a Source Generator, routing can be strongly typed with the necessary strings being generated as a compile-time detail. We're investigating how Source Generators can be used to obviate the need for juggling MSBuild tasks like this, since Source generators don't just offer some performance benefits, but also allows tools to operate at the right level of abstraction.Īnother capability Source Generators can offer is obviating the use of some "stringly typed" APIs, such as how ASP.NET Core routing between controllers and razor pages work. As you might imagine, calling the compiler more than once affects the total time it takes to build your app. Some scenarios involve calling the MSBuild C# task (called CSC) multiple times so they can inspect data from a compilation. Source Generators can improve performance in ways that aren't limited to reflection at run time to discover types as well. ![]() Using source generators could result in some faster startup times, since an action happening at run time today could get pushed into compile time. A generator can analyze your source code and emit the code it needs to "wire up" your app. With a Source Generator, the controller discovery phase of startup could instead happen at compile time. Although this performance penalty isn't enormous, it's somewhat of a fixed cost that you can’t improve yourself in your own app. Although this enables you to write straightforward code with powerful abstractions, it comes with a performance penalty at run time: when your web service or app first starts up, it can’t accept any requests until all the runtime reflection code that discovers information about your code is finished running. A common scenario is to perform some analysis of user code when an app starts up and use that data to generate things.įor example, ASP.NET Core uses reflection when your web service first runs to discover constructs you've defined so that it can "wire up" things like controllers and razor pages. There are countless scenarios for using it. Runtime reflection is a powerful technology that was added to. Source Generators can be an improvement over each approach.
0 Comments
Leave a Reply. |