Try this first!
For anyone suddenly running into this error for seemingly no reason, the error message may be a red herring. You should try deleting all bin and obj folders first before anything else.
- For each project in your solution, locate and delete the obj and bin folders (don't worry, they will be regenerated automatically).
- Right-click the solution and click "Clean Solution"
- Right-click your startup project and click "Rebuild"
For me, the problem occurred while attempting to publish, even though (a) no changes had taken place, (b) it worked the day before, and (c) I was actually using .NET 6.0!
After failing to fix the issue through any recommended solutions, I went ahead and actually removed the warning from the .targets file in the System.Runtime.CompilerServices.Unsafe
package folder (i.e., I suppressed the error), but then the next package in the list started complaining about the same thing.
Solution
Deleting the bin/obj folders and cleaning the solution fixed the issue immediately.