How to use Roslyn Source generators 4.0 in 2021.3?

I want to implement MVVM in Unity 2021.3 using CommunityToolkit.Mvvm-8.3.0.

But CommunityToolkit.Mvvm.SourceGenerators.dll requires Microsoft.CodeAnalysis,Version=4.0.0.0

I found some dll files in “C:\Program Files\Unity\Hub\Editor\2021.3.18f1\Editor\Data\DotNetSdkRoslyn”, which showed that they were version 3.9.

Also in “C:\Program Files\Unity\Hub\Editor\2022.3.45f1\Editor\Data”, there are some 4.3 versions of “Microsoft.CodeAnalysis.dll”

I can use it normally in 2022.3, but for some reason I have to use 2021.3.
Is there any way to help me?

Unity 6’s impending release will coincide with Unity 2021 LTS no longer being supported, which is something to keep in mind.

This GitHub repo (UPM package format) claims to support new compiler versions and mentions some level of support for source generators.

There also used to a Unity-provided package that provided new Roslyn compiler features used for Entities which worked with some older versions, but the last version of the package disables functionality on 2021 (most likely because it had generally equivalent functionality built-in then):
https://docs.unity3d.com/Packages/com.unity.roslyn@0.2/changelog/CHANGELOG.html
It might be possible to re-work this to work on 2021 LTS with a newer SDK’s compiler files.

This seems to be unsupported territory in any case. It may be worth specifically making 2022 LTS your baseline if possible.

I replaced the DotNetSdkRoslyn folder of 2021.3, and then added the 6.0.21 version folder copied from 2022.3 under “2021.3.18f1\Editor\Data\NetCoreRuntime\shared\Microsoft.NETCore.App”.
It worked! ! !
I haven’t found any problems yet. Everything works fine after building and running.