Improved Source Generators and Code Fixers Arrive in .NET Community Toolkit 8.2.1!

Sergio Pedri

We’re happy to announce the official launch of the 8.2.1 release of the .NET Community Toolkit! This new version includes lots of QoL improvements across all libraries, some more performance optimizations to the MVVM Toolkit source generators, new code fixers and improved diagnostics, and more!

.NET Community Toolkit 8.2.1

We want to give a special thank you to all community members that provided valuable feedbacks to help prioritize work items for this new release. Your contributions and bug reports keep helping us making the .NET Community Toolkit even better with each release โ€” you’re the best! ๐Ÿ™Œ

What’s in the .NET Community Toolkit? ๐Ÿ‘€

The .NET Community Toolkit includes the following libraries:

  • CommunityToolkit.Common
  • CommunityToolkit.Mvvm (aka “Microsoft MVVM Toolkit”)
  • CommunityToolkit.Diagnostics
  • CommunityToolkit.HighPerformance

For more details on the history of the .NET Community Toolkit, here is a link to our previous 8.0.0 announcement post.

Here is a breakdown of the main changes that are included in this new 8.2.1 release of the .NET Community Toolkit. This release is mostly focused on incremental improvements: there’s no new features, but lots of tweaks and fixes!

New analyzer and code fixer for [RelayCommand] ๐Ÿค–

The [RelayCommand] attribute (see docs here) can automatically handle asynchronous methods, and it will use the IAsyncRelayCommand interfaces in that case (along with the corresponding async command type). This feature was not easily discoverable by developers though, and many would instead just make their command methods async void. This also meant that they could not leverage all the additional functionality provided by async commands (such as progress reporting and concurrency control).

To help with this, the 8.2.1 version of the MVVM Toolkit ships with a brand new analyzer, which will emit a diagnostic for async void methods annotated with [RelayCommand]. And to make things even simpler to use, there’s also a new code fixer that will automatically refactor the code for you โ€” just click on the lightbulb icon and let Roslyn do the work for you! ๐ŸŽ‰

MVVM Toolkit analyzer code fix

Here you can see the new generated diagnostic showing up for an async void method associated with a command, and its corresponding code fixer UI in Visual Studio featuring a preview of the change. It will also automatically add the necessary using statement at the top of your file, in case Task is not in scope already. And just like with other code fixers in the MVVM Toolkit, you can also easily apply it to all locations in your entire project or solution, with a single click!

Other changes and improvements ๐Ÿš€

  • Fix an AV when indexing a sliced Memory2D<T> instance (#675): in some cases, an access violation could be triggered when indexing elements after slicing a Memory2D<T> instance. That is now fixed, thank you mahalex for reporting this!
  • Fix forwarding attributes with negative enum values (#681): using enum members with negative values will no longer cause issues with the MVVM Toolkit generators for generated observable properties. Thank you n-coelho-cerinnov for reporting this!
  • Fix a generator crash when forwarding invalid attributes (#683): trying to forward attributes that are not referenced correctly will now fail gracefully and produce an easy to understand error message.
  • Fix ObservableValidator generator to detect inherited properties (#616): the generator to validate properties will no longer accidentally ignore properties from base types for the target viewmodel. Thank you dgellow for reporting this!
  • Add warning when using packages.config for MVVM Toolkit (#695): the MVVM Toolkit generators only work when using <PackageReference> (it’s a known limitation of the SDK and it’s by design), but there was previously no clear indication as to why the generators were not running for users trying to use them from a project using packages.config. The MVVM Toolkit now ships with improved diagnostics that will produce a useful warning message in that scenario. Thank you smaugbend for reporting this!
  • Check for cancellation more often in generators (#703): this should result in small improvements in the IDE responsiveness when using the MVVM Toolkit.
  • Remove unnecessary temporary array allocation (#719): another small memory optimizations to the MVVM Toolkit source generators.
  • Handle [ObservableProperty] fields with keyword identifiers (#710): the generator will no longer produce invalid code in case fields annotated with [ObservableProperty] are using keyword identifiers that have been escaped in source (eg. @event). Thank you Get0457 for reporting this!

Note: there is a known issue with source generators in older versions of Roslyn that might cause IntelliSense to sometimes not work correctly for generated members (see #493, and related Roslyn tracking issue). This has finally been fixed in VS 2022 17.7, which is currently in preview. If you’re seeing any issues using the MVVM Toolkit, make sure to try out the latest VS 17.7 Preview!

Other changes โš™๏ธ

You can see the full changelog for this release from the GitHub release page.

Get started today! ๐ŸŽ‰

You can find all source code in our GitHub repo, some handwritten docs on MS learn, and complete API references in the .NET API browser website. If you would like to contribute, feel free to open issues or to reach out to let us know about your experience! To follow the conversation on Twitter, use the #CommunityToolkit hashtag. All your feedbacks greatly help in shape the direction of these libraries, so make sure to share them!

Happy coding! ๐Ÿ’ป

5 comments

Comments are closed. Login to edit/delete your existing comments

  • AVATAR Jake 1

    Thanks Sergio! Really need a MVU Toolkit๐ŸŽ‰

    • saint4eva 0

      Indeed, Windows needs MVU C# framework and MAUI needs that also

  • Max Mustermueller 0

    I’d love to use this but last time I tried, the biggest I have with this toolkit is that when debugging and using VS “go to source” anywhere, it just ends up showing some garbage source generated code. For example the relaycommand. Instead of bringing me to the method where MY code is, it goes to the source generated code NOT containing my code.

    Anyone knows if this has been fixed?

    • saint4eva 0

      Kindly use “go to definition”

  • ๅฎšๅฟƒ ๅˆ˜ 0

    Note: there is a known issue with source generators in older versions of Roslyn that might cause IntelliSense to sometimes not work correctly for generated members (see #493, and related Roslyn tracking issue). This has finally been fixed in VS 2022 17.7, which is currently in preview. If youโ€™re seeing any issues using the MVVM Toolkit, make sure to try out the latest VS 17.7 Preview!

    I want to know that , excepting using the VS17.7 Prev
    how can we update Roslyn only or update something else without VS17.7 PRE? ,LIKE .NET SDK

Feedback usabilla icon