Comments on: Introducing C# Source Generators https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/ Free. Cross-platform. Open source. A developer platform for building all your apps. Sun, 28 Jun 2020 17:43:00 +0000 hourly 1 By: Alexandre Brina https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/comment-page-6/#comment-6687 Sun, 28 Jun 2020 17:43:00 +0000 https://devblogs.microsoft.com/dotnet/?p=27796#comment-6687 This is a very nice feature but source code written in strings doesn’t look right. Please let code be written as code, with all the IDE support like syntax coloring, code completion, debugging, etc. I mean, it can in fact be strings, allowing interpolation, etc, but written in a regular file, template-like, that let us see the code right away “as code” instead of string-building.

]]>
By: Jinming Mu https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/#comment-6567 Thu, 11 Jun 2020 11:33:12 +0000 https://devblogs.microsoft.com/dotnet/?p=27796#comment-6567 In reply to Cristhian Fernandez.

source generators you can modify/rewrite existing code” Are you sure? so far all i know is that source generator only can add code. Could you give a example. thank you

]]>
By: Antão Almada https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/comment-page-6/#comment-6428 Sun, 31 May 2020 23:23:54 +0000 https://devblogs.microsoft.com/dotnet/?p=27796#comment-6428 I ported my source generator project from using Uno.SourceGeneration to using the new C# Source Generators. It was actually very simple as these are very similar.

It’s an internal generator that automatically generates lots of overloads to my types so that value types are not boxed. You can find the code at https://github.com/NetFabric/NetFabric.Hyperlinq/tree/cb76d29c8d9b91935079fa4d226e393b01b1f03f/NetFabric.Hyperlinq.SourceGenerator

I’m planning to add a public generator to generate binding methods.

]]>
By: Mourad CHIBANE https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/comment-page-5/#comment-6254 Sat, 23 May 2020 02:42:28 +0000 https://devblogs.microsoft.com/dotnet/?p=27796#comment-6254 In reply to Dr. Chris of Rain.

For debugging you can simply add :

System.Diagnostics.Debugger.Launch();

and at compile time you will be able to attach your debugger.

]]>
By: David Lewis https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/comment-page-6/#comment-5789 Mon, 18 May 2020 17:26:59 +0000 https://devblogs.microsoft.com/dotnet/?p=27796#comment-5789 In reply to Jinming Mu.

T4 templates don’t have access to your programs structure at compile time. even when running from the same assembly. Source generators do have access to that structure and are brought in as a library rather than copy pasting into your source code. since source generators allow for compile time introspection and code generation there are opportunities to do things like IOC containers with zero runtime reflection overhead and I can see things like automapper and ASP.Net being rewritten using this technology to be MUCH faster.

]]>
By: Jinming Mu https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/comment-page-6/#comment-5746 Wed, 13 May 2020 23:51:28 +0000 https://devblogs.microsoft.com/dotnet/?p=27796#comment-5746 This is a very promising feature. but so far It is similar with T4 if it could not manipulate the exiting code.
and It is unnecessary if just for boot faster. AOP it will be one of its major features but not all.

]]>
By: Pierre Picard https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/comment-page-6/#comment-5740 Wed, 13 May 2020 13:30:31 +0000 https://devblogs.microsoft.com/dotnet/?p=27796#comment-5740 Hi,
This is an interesting feature indeed, but there was another way to do the same thing. Let’s consider the C++ concept of constexpr. If there was a similar feature in C# like “compile time blocks” which would contain code executed at compile time, it would allow to do the same thing as Source Generators for most cases. I understand that it would be much more work for the team working on the C# compiler, but for an application developper it would have been much much more simple and accessible than having to: rely on Roslyn to walk along ASTs, building strings containing C# code, and create an external analyzer package.

]]>
By: michael Lang https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/comment-page-5/#comment-5731 Tue, 12 May 2020 05:24:34 +0000 https://devblogs.microsoft.com/dotnet/?p=27796#comment-5731 A long time ago, when .NET 1.0 was the new kid on the block, Code Generators (not to be confused with Source Generators) were all the rage. For a long time, for many reasons, I had no interest them. I only became sold on Code Generators the day MS introduced partial classes in .NET 2.0, however just when I was finally on board, they fell out of favour. With .NET 2.0 they also introduced Entity Framework, and with that Code Generators such as MyGeneration and CodeSmith were suddenly yesterdays news. The majority of people were only generating code to talk to the database, so they saw no need for CodeSmith and MyGeneration, which essentially was replaced by in built VS scripts and T4.

It feels like a dark secret, but I’m actually still using MyGeneration. I use it to generate all our EF classes and repositories based on database meta data and hand coded MyGeneration scripts. I use MyGeneration over T4 as it just seems a whole lot easier and more powerful.

I think Code Generation is great for bridging application domains that are physically separated yet tightly coupled such as the example you’ve given above with a client generating code based on meta data for a web service, or generating EF code based on a database schema, or going the other way and generating SQL scripts base on EF code etc.

Source Generators seem like something you could use to create your own scripting language like Code Smith, MyGeneration or T4… rather than a scripting language in itself. It’ll be interesting to see where Microsoft runs with this, and what it gets used for. Is Microsoft already using this internally for anything?

]]>
By: Dr. Chris of Rain https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/comment-page-5/#comment-5723 Sun, 10 May 2020 23:29:45 +0000 https://devblogs.microsoft.com/dotnet/?p=27796#comment-5723 I ported a serializer library from a project of mine to use source generators. Here are some of my thoughts:

  1. Debugging support is desperately needed. At minimum it would be good to see the details of exceptions that crashes the source generator. But being able to easily attach a debugger would make life a lot easier.

  2. Roslyn’s diagnostic system seems to be pretty thorough. But if you are developing a library, sometimes you just want something quick and dirty. It would be good if there was a simple way to output debug logs, warnings, errors, etc.

  3. Roslyn’s API is massive. This is somewhat understandable because it is a compiler. But it can be pretty hard to find anything. For example, presumably there code somewhere that creates a mapping between the Accessibility enum and its string representations. But I couldn’t find it.

  4. I suspect that for a lot of source generator libraries, the semantic model will be the most useful. So, it is a little strange that it isn’t presented front and center in the source generator interface. I probably wouldn’t have been able to figure out how to access the semantic model without looking at the samples.

All that being said, I think this is a pretty cool feature and I am definitively looking forward to its official release. Keep up the good work!

]]>
By: Scott Holodak https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/comment-page-5/#comment-5705 Fri, 08 May 2020 15:25:57 +0000 https://devblogs.microsoft.com/dotnet/?p=27796#comment-5705 By any chance can the generator live in a netstandard2.1 library? I need to consume a library that uses IAsyncEnumerable with await foreach‘s.

]]>