Related
I'm getting a:
type or namespace name could not be found
error for a C# WPF app in VS2010. This area of code was compiling fine, but suddenly I'm getting this error. I've tried removing the Project Reference and the using statement, shutting VS2010 and restarting, but still I have this issue.
Any ideas why this might be occurring, where it seems like I'm doing the right thing re Reference & using statement?
I also noted in VS2010 that intellisense for that namespace is working ok, so it seems like VS2010 has the project reference and is seeing the namespace on one hand, but during compile doesn't see it?
This can be the result of a .Net framework version incompatibility between two projects.
It can happen in two ways:
a client profile project referencing a full framework project; or
an older framework version targeting a newer framework version
For example it will happen when an application is set to target the .Net 4 Client Profile framework, and the project it references targets the full .Net 4 framework.
So to make that clearer:
Project A targets the Client Profile framework
Project A references Project B
Project B targets the full framework
The solution in this case is to either upgrade the framework target of the application (Project A), or downgrade the target of referenced assembly (Project B). It is okay for a full framework app to reference/consume a client profile framework assembly, but not the other way round (client profile cannot reference full framework targeted assembly).
Note that you can also get this error when you create a new project in VS2012 or VS2013 (which uses .Net 4.5 as the default framework) and:
the referencing project(s) use .Net 4.0 (this is common when you have migrated from VS2010 to VS2012 or VS2013 and you then add a new project)
the referenced projects use a greater version i.e. 4.5.1 or 4.5.3 (you've re-targeted your existing projects to the latest version, but VS still creates new projects targeting v4.5, and you then reference those older projects from the new project)
Reinstalling nuget packages did the trick for me. After I changed .NET Framework versions to be in sync for all projects, some of the nuget packages (especially Entity Framework) were still installed for previous versions. This command in Packages Manager Console reinstalls packages for the whole solution:
Update-Package –reinstall
I've no idea why this worked, but I removed the project reference that VS2015 was telling me it couldn't find, and added it again. Solved the problem. I'd tried both cleaning, building and restarting VS to no avail.
When building the solution I was getting the same error (type or namespace ' ' could not be found). Below it I saw a warning stating that "the reference could not be resolved" and to make sure "the assembly exists on disk".
I was very confused, because my DLL was very clearly in the location that the reference was pointing to. VS didn't seem to highlight any errors, until I tried to build the solution.
I finally realized the problem (or at least what I suspect was the problem). I was building the library file in the same solution. So even though it existed on the disk, it was being rebuilt in that location (somehow in the process of the library getting rebuilt my other project - in the same solution - that referenced the library must have decided that the library didn't exist)
When I right-clicked on the project and built that only, instead of the entire solution, I didn't get the error.
To fix this problem I added the library as a dependency to the project that was using it.
To do this:
I right-clicked on my Solution in the Solution Explorer and selected
"Properties"
Then in "Common Properties" I selected "Project Dependencies".
Then in the Projects drop-down menu I selected the project that
relied on the library, and
Checked the box next to the library found under "Depends On"
This ensures that the library project gets built first.
First I would verify that your project's generated information isn't corrupt. Do a clean and rebuild on your solution.
If that doesn't help, one thing I've seen work in the past for designer issues is opening up a windows forms project, then closing it again. This is a little chicken-entrails-ish, though, so don't hold your breath.
A trickier situation I ran into was:
Project one targets the 4.0 full framework with Microsoft.Bcl.Async package installed.
Project two target the 4.0 full framework but would not compile when reference a Project one class.
Once I installed the Async NuGet package on the second project it compiled fine.
In my case, I find the reference in the VisualStudio have a triangle, and a exclamation mark as this image,
then, I right click remove it, and add the dll reference correctly again, the problem was solved.
I had a similar issue: The compiler was unable to detect a folder inside the same project, so a using directive linking to that folder generated an error. In my case, the problem originated from renaming the folder. Even though I updated the namespace of all the classes inside that folder, the project info somehow failed to update. I tried everything: deleting the .suo file and the bin and obj folders, cleaning the solution, reloading the project - nothing helped. I resolved the problem by deleting the folder and the classes inside, creating a new folder and creating new classes in that new folder (simply moving the classes inside the new folder didn't help).
PS: In my case I was working on a web application, but this problem may occur in different types of projects.
This one worked for me. In your class, where the class name is defined, eg: Public class ABC, remove one character and wait a little. You error list will increase because you have changed the name. Now put back the character that you have typed. This worked for me, hopefully it will work for you too. Good Luck!!!
[Facepalm] My issue was that I had added the dependency in the C++ way of doing things.
Go to the project that won't build, open up the 'References' folder in Solution Explorer, and see if your dependency is listed.
If not, you can 'Add Reference' and choose the dependency on the Projects tab.
Boom Shankar.
Check the Build Action of the .cs file containing the missing type. Make sure it's C# compiler.
Click on the .cs file containing the missing type.
Press F4 to bring up Properties.
Make sure Build Action is set to C# compiler.
Before:
After:
It even happens in Visual Studio 2017.
Restart Visual Studio
Clean project that fails to build.
Rebuild the project.
Had the same errors, my story was following:
after bad merging (via git) one of my .csproj files had duplicated compile entries like:
<Compile Include="Clients\Tree.cs" />
<Compile Include="Clients\Car.cs" />
<Compile Include="Clients\Tree.cs" /> //it's a duplicate
If you have a big solution and more than 300 messages in the errors window it's hard to detect this issue.
So I've opened damaged .csproj file via notepad and removed duplicated entries. Worked in my case.
I had same problem as discussed: VS 2017 underlines a class in referenced project as error but the solution builds ok and even intellisense works.
Here is how I managed to solve this issu:
Unload the referenced project
Open .proj file in VS ( i was looking for duplicates as someone suggested here)
Reload project again (I did not change or even save the proj file as I did not have any duplicates)
We had a weird case of this that I just fixed in a solution. There was a hidden/whitespace character in front of a "using" statement in the main project. That project would build fine and the website worked fine, but the unit test project that referenced it could not be built.
I encountered this problem when upgrading existing projects from VS2008 to VS2012. I found that two projects (the only two that I created) were targeting different .Net Frameworks (3.5 and 4.0). I resolved this on the Application tab of the projects by making sure that both projects had ".NET Framework 4" in the Target Framework box.
In my case I had a Class that was listed in the proper source folder, but was not registering in Solution Explorer. I had to do right click the project > Add Existing item and manually select that Class it said it was missing. Then everything worked fine!
I know its old, but I've found the same issue. My project did build, I then updated Visual Studio to the latest & the project wouldnt build as it couldnt find a type definition from a separate assembly. The other assembly built OK, the main project referenced it correctly & nothing had changed since it built OK.
I cleaned the whole solution & rebuilt it, it failed. I built the assembly on its own, it built OK. The project didnt build. I cleaned & built multiple times, and it failed. I then called a colleague to look at it, when I built with him watching, it all built OK.
I think Visual Studio tooling is the problem, especially as I just updated it.
In my case, I unload the project, then:
Opened myProject.csproj and update the ToolsVersion="4.0" to ToolsVersion="12.0"(I'm using vs 2017)(using Paulus's answer https://stackoverflow.com/a/64552201/1594487).
Deleted following lines from the myProject.csproj:
<Import Project="..\packages\EntityFramework.6.4.0\build\EntityFramework.props" Condition="Exists('..\packages\EntityFramework.6.4.0\build\EntityFramework.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
And the problem solved.
You might also try eliminating the code you think you're having problems with and seeing if it compiles with no references to that code. If not, fix things until it compiles again, and then work your suspected problem code back in. Sometimes I get strange errors about classes or methods that I know are correct when the compiler doesn't like something else. Once I fix the thing that it's really getting hung up on, these 'phantom' errors disappear.
I know this is kicking a dead horse but I had this error and the frameworks where fine. My problem was basically stating that an interface could not be found, yet it build and accessed just fine. So I got to thinking: "Why just this interface when others are working fine?"
It ended up that I was actually accessing a service using WCF with an endpoint's interface that was using Entity Version 6 and the rest of the projects were using version 5. Instead of using NuGet I simply copied the nuget packages to a local repository for reuse and listed them differently.
e.g. EntityFramework6.dll versus EntityFramework.dll.
I then added the references to the client project and poof, my error went away. I realize this is an edge case as most people will not mix versions of Entity Framework.
Adding my solution to the mix because it was a bit different and took me a while to figure out.
In my case I added a new class to one project but because my version control bindings weren't set I needed to make the file writable outside of Visual Studio (via the VC). I had cancelled out of the save in Visual Studio but after I made the file writable outside VS I then hit Save All again in VS. This inadvertently caused the new class file to not be saved in the project..however..Intellisense still showed it up as blue and valid in the referencing projects even though when I'd try to recompile the file wasn't found and got the type not found error. Closing and opening Visual Studio still showed the issue (but if I had taken note the class file was missing upon reopening).
Once I realized this, the fix was simple: with the project file set to writeable, readd the missing file to the project. All builds fine now.
I had the same issue. One night my project would compile the next morning ERRORS!.
I eventually found out that visual studio decided to "tweak" some of my references and point them elsewhere. for example:
System.ComponentModel.ISupportInitialize somehow became "blahblah.System.ComponentModel.ISupportInitialize"
Quite a rude thing for vs to do if you as me
My case was same as discussed here but nothing solved it until I've removed the System.Core reference from the references list (Everything worked fine without it)
hope it will help someone because this issue is quite frustrating
To solve this issue it can also help to delete and recreate the *.sln.DotSettings file of the associated solution.
Ok, years later using VS 2017 .NET Core 2.2 Razor Pages I feel this answer might help someone.
If it was a snake it would have bit me.
I was throwing stuff around, changing names, renaming Models, and all of a sudden I got this error:
Error CS0246 The type or namespace name 'UploadFileModel' could not be
found (are you missing a using directive or an assembly reference?)
This was underlined in red in my .chstml Razor Page. (not underlined after fix):
#page
#model UploadFileModel
So, finally, and luckily, I found the code from someone else that I had originally used, and low and behold, the namespace did not include the .cshtml file name!!!
Here is my bad dummy error spank myself with the page name in the namespace:
namespace OESAC.Pages.UploadFile
{
public class UploadFileModel : PageModel
{
What my original code had and all I had to do was delete the page name from the namespace, UploadFile:
namespace OESAC.Pages
{
public class UploadFileModel : PageModel
{
And low and behold, all the errors disappeared!!
Silly me. But you know, MS has made this .NET C# MVC stuff really confusing for us non-computer scientists. I am constantly tripping on my shoelaces trying to figure out model names, page names, and syntax to use them. It shouldn't be this hard. Oh well. I hope error and solution helps someone. The error was right, there is no Namespace named "UploadFileModel" haha.
In my case the problem was that after changing namespace to exactly same as is in another project (intentionally), the name of assembly was changed as well by VS, so there were two assemblies with same name, one overriding the other
Had the same problem after merging some new code into a vs2019 project.
Restarted VS, unloaded and reloaded projects, ensured all projects in solution had same ToolsVersion= and TargetFrameworkVersion. None of this helped.
I had a case of project Substrate, failing to find namespace Skin (in project Skin).
Finally I opened up Substrate.csproj and checked all the ProjectReference Include entries. The others were there, but no reference to Skin, even though Skin did show up in the check box of the little project dependencies dialog. So the project dependencies dialog took the check box for Skin (and saved it somewhere) but did not alter Substrate.csproj. I then added the ProjectReference Include manually, ensuring I had the correct path and GUID for the skin project.
<ProjectReference Include="..\Skin\Skin.csproj">
<Project>{1ad4b5d7-5014-4f5f-983e-2c59ac0e0028}</Project>
<Name>Skin</Name>
</ProjectReference>
I then saved the Substrate.csproj and the problem was solved. So as others have said this is a problem with VS tooling
In my case I had a file built by external dependency (xsd2code) and somehow its designer.cs files were not processed by VS correctly. Creating a new file in Visual Studio and pasting the code in it did the trick for me.
To anyone that is getting this error when they try to publish their website to Azure, none of the promising-looking solutions above helped me. I was in the same boat - my solution built fine on its own. I ended up having to
Remove all nuget packages in my solution.
Close and reopen my solution.
Re-add all the nuget packages.
A little painful but was the only way I could get my website to publish to Azure.
I can't run my unit tests.
I have the next error:
Your project does not reference ".NETFramework,Version=v4.6.2"
framework. Add a reference to ".NETFramework,Version=v4.6.2" in the
"TargetFrameworks" property of your project file and then re-run NuGet
restore.
In app.config:
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
</startup>
In Project > Properties > Application > TargetFramework (.NET Framework 4.6.2)
How can I fix it?
Please make the next steps
Clean solution
Clean folder "packages"
Delete folder "bin"
Delete folder "obj"
I experienced similar issue, but with v4.7.2. Namely, I kept getting build log message like this:
error : Your project does not reference ".NETFramework,Version=v4.7.2" framework. Add a reference to ".NETFramework,Version=v4.7.2" in the "TargetFrameworks" property of your project file and then re-run NuGet restore.
Despite the fact that it looked similar, none of the above proposed steps worked for me. I kept seeing this message after each build. Nothing seemed to be able to help.
In fact, the problem was related to that, due to migration, I had to put two projects in one folder of code. One of them was targeted at .Net Core, another at .Net Framework, both referenced same .Net Standard libraries. Apparently, they share the same obj folder where Core projects put project.assets.json file. Actually, exactly this file interferres with the Framework project preventing its normal build. Seems even if you performed Migrate from packages.config to PackageReference... which was recommended as one of possible solution.
You can try to fix the issue by putting the following snippet into your Framework project file:
<Project>
...
<PropertyGroup>
<BaseOutputPath>$(MSBuildProjectDirectory)/out/$(MSBuildProjectName)/bin</BaseOutputPath>
<BaseIntermediateOutputPath>$(MSBuildProjectDirectory)/out/$(MSBuildProjectName)/obj</BaseIntermediateOutputPath>
</PropertyGroup>
...
</Project>
It immediately worked for me, it was only later when I attentively read why we need it and why it works. I unexpectedly found it in part 2 of Migrating a Sample WPF App to .NET Core 3 under Making sure the .NET Framework project still builds section. BaseOutputPath and BaseIntermediateOutputPath msbuild variables can be found there, not sure if they are documented well anywhere.
That happened to me when opening a VS2015 project in VS2017. Deleting the project.assets.json from the obj folder did the trick.
Anyway, the Framework from the message was missing in the file, I did not add it there though, went with deleting it.
git clean -xdf
That should do the trick. It worked for us also in Jenkins. (we simply replayed the failed build with a modified script that ran git clean first).
For some reason MSBuild / Visual Studio get confused when switching between branches that target different versions of .NET Framework, so I had to do git cleans regularly while switching between branches.
The file that was causing issue for me was obj/project.assets.json inside project folder. Deleting it and rebuilding the project worked.
I had deleted the obj folder and rerun the build after choosing the target framework required in the property window it worked for me.
I up-voted Larissa but I thought it might be helpful to know how I got into this. I added a .net standard project file to my build (we target lots of platforms) and it produced the debris found in the obj folder. When the android sanity build came around, it threw up on the obj folder. My solution was to clean that folder out as a pre-build step. This is a difficult problem because it was working just fine for years...needle meet haystack.
For my case, delete the .pkgrefgen/ folder under the project folder works, it contains a file project.assets.json that refer to old .net framework
I ran into the same thing with .net 4.71. In my case, I simply migrated from packages.config to "package references" per
Migrate from packages.config to PackageReference
... and it fixed my issue. For me, I was going to do this anyway, so if you're going this way already, I'd just skip the above and migrate to package references.
Renaming the project solved the error for me. The issue happened after I created .NET Core project, then I deleted it and created a .NET Standard one with the same name. Obj folder was not present at all. Deleting bin folder, packages, clean and rebuild solution and getting latest with override did not help.
I have not tried this, but this thread proposed workaround is to include into csproj tag:
<ResolveNuGetPackages>false</ResolveNuGetPackages>
I am using a very old .NET project, and it was working fine until it stopped all of a sudden. Upgrading Visual Studio fixed for me thou.
On VS2019 I had to follow the error message and edit the project.json file that was in the project directory.
was ".NETFramework,Version=v4.0": {} // whatever the copied project was set to
now ".NETFramework,Version=v4.7.2": {} // set to whatever the current build is set to
Problem: In VS2017. Missing reference to .netframework 4.5.2, even though it was referenced as the target framework.
My solution: Verified framework was installed and restarted machine. After a git clean and simply right clicking on the solution in the explore and 'Restore nuget packages' did the trick.
For whatever reason, I got this build error in VS2022.
The same build in VS2019 was successful.
I had the same issue in CI/CD process, when i had upgraded .net framework version from 4.6.1 to 4.7.2 which worked fine locally without any other modification.
However, the jenkins 'slave' node where the build was actually getting generated had some issue with nuget restore and it was not able to pick the latest build for some reason.
Logged into jenkins slave machine/node (basically the machine which jenkins uses to create the build/artifact), go to deployment path and then try deleting projects old builds along with .nugets folder and trigger CI/CD process again worked for me.
This error also occurs if you have removed an old SDK that provided nuget packages, but it is still referenced in your package sources list under Nuget manager/settings. Remove the nuget package source no longer in use to fix this. Otherwise, Visual studio on building will create the project.assets.json file with a reference to the old sdk and if the path is not there, you get the OP's error.
In my case, I had DevExpress 20.2 in my list which I removed to resolve this issue.
I have a project that i have not run for some while, build with dotnet core 1.1.2 dependencies.
in the meanwhile I have updated visual studio, possible installed some dotnet core stuff for 2.0 and my application do not run anymore.
InvalidOperationException: Can not find assembly file Microsoft.CSharp.dll at 'C:\dev\EarthML\EarthML.Mapify\src\EarthML.Mapify.Portal\bin\Debug\net462\win10-x64\refs,C:\dev\EarthML\EarthML.Mapify\src\EarthML.Mapify.Portal\bin\Debug\net462\win10-x64\'
Microsoft.Extensions.DependencyModel.Resolution.AppBaseCompilationAssemblyResolver.TryResolveAssemblyPaths(CompilationLibrary library, List<string> assemblies)
What would I do to start figuring out why it dont work?
This seems to be a bug with the current version of Visual Studio (15.3). The underlying problem is when the Views are trying to build, they are not referencing the GAC, and thus, have none of their dependencies.
While only a work-around, your best bet when encountering this problem is to copy over the missing DLLs. I set them up as components in my project with a copy action. Once it gets fixed (currently Triaged: https://developercommunity.visualstudio.com/content/problem/96927/runtime-error-after-upgrading-to-vs-153-invalidope.html), then it's a matter of deleting them.
EDIT
Alternately, according to https://github.com/dotnet/sdk/issues/1488, adding this as a reference to the project should work instead of adding all the dependencies:
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="2.0.0" />
Have you included the Microsoft.CSharp.dll reference? Expand your project, right click on References, click Add Reference, search Microsoft.CSharp, check the box, click OK.
I upgraded the application to dotnet core 2.0 and razor pages and got around the issue.
I had <DependsOnNETStandard>true</DependsOnNETStandard> and I had to change it to <DependsOnNETStandard>netstandard1.6</DependsOnNETStandard>
<PropertyGroup>
<TargetFramework>net462</TargetFramework>
<RuntimeIdentifier>win7-x64</RuntimeIdentifier>
<IsServiceFabricServiceProject>True</IsServiceFabricServiceProject>
<AssemblyName>MyProject</AssemblyName>
<Platforms>AnyCPU;x64</Platforms>
<DependsOnNETStandard>netstandard1.6</DependsOnNETStandard>
</PropertyGroup>
Source: From neman on GitHub Isue
In my C# project, a few .NET Standard classlib's support a Core console application and a Winforms .net 4.7 program. Yesterday I opened my project, one of the .NET standard dll's gave above error, leaving references with an exclamation mark, same for all libs that depend on it in the solution ! It looked very alarming.
I kept working on my solution, though.. I use Visual Studio 15.8.1 here, the issue seems to be a spook. A fake warning. The exclamation marks remain while working on the project, but nothing happens, it does not disturb anything or interrupt rebuilds. The assembly runs as expected.
Few minutes ago I solved it, by removing the Microsoft.CSharp reference, then rebuild all. It was in only one library, so the rebuild solved all exclamation marks in the solution. The reference has gone away, clear sky.
I had similar issue where in a .net standard was using "dynamic" type which is there in "Microsoft.CSharp". The project was building from Visual studio but was failing from azure devops release pipeline. adding reference from nuget solved the problem.
Install via https://www.nuget.org/packages/Microsoft.CSharp/
Reference (Credits): https://mikaelkoskinen.net/post/net-core-standard-missing-compiler-required-member
clean the solution and rebuild it .
if not worked create new project then copy dll and paste .
good luck .
I'm coming to you for help for a problem I have.
I posted the problem in the CLI of dotnet but it's been 10 days+ as of this moment of writing and I have no answer.
I have a project with some dependencies on .NET 4.5 and everything worked properly in RC1 and RC2. Now trying to move to RTM (1.0), I have an error that the project can't be made runnable because the runner can't find a specific DLL. The project compiles fine. Doing dotnet run fails
It looks for the DLL in a library that my app (site) uses, specifically XXX.v15.4, version=15.4.0.0, culture=neutral, publickeytoken=b881323kd82k.dll but it does not exist. However, XXX.v15.4.dll exists fine.
Copying XXX.v15.4.dll into XXX.v15.4, version=15.4.0.0, culture=neutral, publickeytoken=b881323kd82k.dll makes the project run.
The DLL that it's trying to load is a third party library used in a library that the app (site) uses.
Any idea of how I could make the project runnable?
Thank you!
I faced the same problem, and thought that problem lied in project.fragment.lock.json or in project.json, but in my case the problem was with incorrect project.lock.json files.
It seems like after addition of some new Nuget packages these files need to be recreated.
When i deleted these files, packages have been restored correctly, and the solution successfully built.
Hope it could help someone.
I am having this same issue and it seems like what is happening is some of the dll's are set to read-only post build. SO the next time you build they can not be built.
What I have done to solve the issue is removed all read-only settings from the root folder of my solution.
So for example, if this is your folder structure in windows:
C:\Users**yourname**\Documents\visual studio 2015\Projects\ProjectName
Right-click ProjectName and remove read-only settings recursively.
In my case this has solved the issue and I hope it helps you
I have a solution with 3 projects:
ParsersBase, that defines an interface IParseRule
ParsersLibrary, that has a reference to ParsersBase and defines a class HtmlImageUrlParseRule : IParseRule
ParsersLibraryTest, that has a reference to ParsersBase and ParsersLibrary and defines a test class with some test methods
When I'm trying to build it, I get a warning:
Reference to type 'AVSoft.ParsersBase.IParseRule' claims it is defined in 'c:\Users\Tim\Dropbox\projects\Image Downloader\ParsersLibrary\bin\Debug\ParsersLibrary.dll', but it could not be found
Why is VS trying to find AVSoft.ParsersBase.IParseRule in ParsersLibrary.dll? ParsersLibraryTest has a reference to ParsersBase; it just doesn't make any sense.
Another way this could happen is if you're using several NuGet packages where one, probably central, package has been updated but some additional functionality packages haven't been.
To work through my recent example - the error was "Reference to type 'ConsumerSubscriptionConfigurator<>' claims it is defined in 'MassTransit', but it could not be found". This is because we had updated MassTransit from 2 to 3, but we had not updated some of the other optional packages (MassTransit.log4net and MassTransit.Autofac) to the same version number. It appears as if assembly redirection had kept everything working until we tried to use one more additional feature.
This error seems to cover a variety of scenarios. In my case, closing and re-opening Visual Studio was the trick. After restarting Visual Studio, I was able to build the project as expected.
I had a similar problem. The site was running a cached version of the dll and not the one I had deployed to the bin directory. I cleared the temporary asp.net folder contents and this solved the issue.
It was my fault, I had a ParsersLibrary project at the start and then renamed it to ParsersBase, but I didn't rename an assembly name, then I added a ParsersLibrary project again.
So, two projects had the same assembly name and it's not very good, is it? :) Assemblies overlap each other, so I have this error.
I had the similar problem: Reference to type 'Func<>' claims it is defined in 'mscorlib', but it could not be found. The problem was following: my solution had Target Framework = 3.5 and I added a reference to Microsoft.Practices.Prism v 4.0 which is built against the framework 4.0.
After changing target framework to 4.0 it worked
It looks like things are a bit easier now than they were before.
As other answer(s) have basically already stated, this error can result from an older version of the same NuGet package not having some of the newer types in it. While in production, this is generally managed through proper versioning, in development, you may end up reusing the same version number when making changes. And that's a likely place where this problem can arise.
To fix this, you can often just clear the cache by doing the following:
In Visual Studio, go to Tools > NuGet Package Manager > Package Manager Settings.
In the pop-up menu, navigate to NuGet Package Manager > General.
In the options on the right, click Clear All NuGet Cache(s).
I hit this exception today. The problem in my case was I had some.package v2.1installed in my host and some.package v2.3 installed in other projects. Update-Package on the host project to v2.3 fixed the issue.
I my case, I tried to test a WPF project with a .NET Core (3.1) test project which could not reference the needed WindowsBase.dll.
Updating/consolidating packages didn't help. Even a clean repo and a restart of Visual Studio didn't solve it for for me.
But rebooting did fix the problem!
#binki's comment helped me;
deleting all .vs, bin, and obj folders, and then reopening the project
ParsersLibraryTest needs to reference ParsersBase. The second part of the error should read "You must add a reference to assembly 'ParsersBase..."
I tried all of the above answers but none resolved my issue.
In the end, I checked in my latest code (GIT), then recloned the repository in a different location.
Not ideal, but at least problem solved.
I had the similar problem: Reference to type 'Func<>' claims it is defined in 'mscorlib', but it could not be found. I have a lib of .Net 4 that was referenced by a .Net 3.5 program. After upgrading both to 4.61 it worked.
Seems like Func<T> is missing in .Net 3.5 and just upgrading that sample app would have been enough.
Further exp: Someone had added a signature in the library project (.Net 4) that uses a Func<T> parameter. But the sample program (3.5) already existed and ran fine so far. But at time of recompilation a dependency of a dependency clashed. Because mscorelib-3.5 had been already loaded. All happens at compilation time.
The only way I could overcome this error was to force uninstall of all nuget packages related and then reinstalling them. Sad but true.
I've just struggled with this error for a while now and finally get around it.
This is how to re-produce it and how I fixed it.
The problem was:
The packages were referenced by Right clicked -> add refernece -> Browse (choose). Then were added again as NuGet packages.
The solution was:
Remove the added references.
Remove the installed packages from .csproj.
Re-install the required packages from NuGet package mangager.
Close Visual Studio and re-open it.
Clean Project.
Build Project.
Note: If you couldn't remove the referenced files (no Remove option on right click) try close Visual studio and re-open it. Or delete or move the dll that were referenced then try again.
For me, I had chosen incorrect project, I was creating a class library project, I had to chose "Class Library (.Net framework)" but I had chosen "Class Library (.Net standard)"
Replacing the same resolved the issue.
The problem was following: my solution had Target Framework = 3.1 and I added a reference to Microsoft.EntityFrameworkCore.SqlServer v 2.0 which is built against the framework .
I had this problem with one of my library projects inside of a solution, after I switched from .NET Framework to .NET Standard. Eventually I just removed the project reference and added it again inside the application project that was reporting the problem. Oddly enough, the only thing that changed was project GUID switching to lower case from the previous upper case.