Source Generators in C#

Play Source Generators in C#

The Discussion

  • User profile image
    John Melville-- MD
    So when I first saw C# attributes (in July of 2002) source generators was what I thought they did. I was very disappointed to discover that attributes are only decorations that do not affect the compile in some way.

    C# has needed (and not had) a good metaprogramming solution for the past 18 years. Thank you for finally adding a much needed feature. I have tried writing a INotifyPropertyChanged generator of my own and it is pretty straightforward.

    Are you going to make it easier to tie a generator to a nuget library. I would like, for example to write a generator which makes a class inherit from a interface in a third nuget package. The generator could then implement the interface, which gives us a mixin model.
  • User profile image
    I'll have to try this out in the context of generating C++ code. We're building a .NET API for a predominantly native C++ product, and we tend to end up with about 3 representations of a given class. Some of that code is actually based on some data in an XML file, and we have in some cases used T4 to automate that.

    It'd be nice to use this to be able to generate not just the C# code, but the C++ code as well (even if we have to build up the code string ourselves and emit it to a file).
  • User profile image

    This would have been useful for a project I am working on with auto mapping between database object and service object with the same property on both sides.

  • User profile image
    Curious, when I was watching this video, I noticed that the generated properties were instantly available to the developer via Intellisense. My source generator example generates an extension method that is valid, the code compiles, it executes as expected at runtime, but in the IDE, it still shows a red squiggle, and I can't get it to show via Intellisense (I have 16.8.0 Preview 1.0). Seems like the developers were using an "Int Preview" build of VS, so maybe that's coming and hasn't been made available in the Preview version of VS just yet?
  • User profile image
    Talk so much... 😒 as always. My main language is not English. So that I need to focus so much on the lectures. What about having some demo? 😡
  • User profile image
  • User profile image
    @JasonBock:Normally, an extension method - if in another namespace, isn't found by VS. You have to add a using.. statement
  • User profile image

    'Way back in ancient times there actually used to be a feature called 'macros' which was essentially a VBS back-end that you could use to write programs to generate code that would slap code into your code depending on whatever you wanted them to depend on. I used to have tons of VBS macros in there. Then Microsoft ripped out that VBS macro facility entirely and I've since been stuck doing it the hard way - by hand. Having a similar facility built back into the system should be highly entertaining. Those macros used to save me a ton of time. Having the ability to easily share them with the team will also be a big bonus. I'm sure others will have ideas, too.

  • User profile image
    but can I see generated code? I didn't see it in this video, maybe I missed it?

    other question: source generators it's something like type providers in f#? or completely different?
  • User profile image
    These generators look great! for users! But I'm completely lost at the author part. Looking forward that it can be easier to create a generator for an ordinary developer😅.
  • User profile image
    A A

    Q: Is it possible to add a real file to the solution?
    I mean, I know it's autogenerated, but I want to make a changes to the auto generated class!

Add Your 2 Cents