I was getting this error on a unit test project. It was odd because this was the result of a refactor exercise, everything was working before
There were 2 things I did, and I can not say exactly which solved the problem, but I just spent a 1/2 day on this so I want to maybe save someone else time.
I introduced a subclass in a new project that was .NET Framework 4.8.1 Other projets depending on this new project had lower .Net versions. I brought them all up to 4.8.1 (I dont think that exercise caused or resolved the problem. )
I also had a unit test project to test the new class. Somehow (I assume I did it) the project reference to Microsoft.CSharp had been removed.
Chatgpt suggested ensuring this reference. I added a reference in the solution explorer.
That solved the problem for me.