By Greg


2010-07-21 23:42:53 8 Comments

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?

30 comments

@daveD 2020-07-17 11:02:36

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.

@Dave Cousineau 2020-04-01 01:12:52

Struggled with this for a while, and not for the first time. In times past it usually was a configuration mismatch, but this time it wasn't.

This time it has turned out to be that Auto-Generate Binding Redirects was set to true in my application.

Indeed, if I set it to true in my library, then my library gets this error for types in the Microsoft.Reporting namespace, and if I set it to true in my application, then my application gets this error for types from my library.

Additionally, it seems that the value defaults to false for my library, but true for my application. So if I don't specify either one, my library builds fine but my application gets the error for my library. So, I have to specifically set it to false in my application or else I will get this error with no indication as to why.

I also find that I will get this message regardless of the setting if a project is not using sdk csproj. Once I convert to sdk csproj, then the setting makes a difference.

Also, in my case, this all seems to specifically have to do with the Microsoft.ReportingServices.ReportViewerControl.Winforms nuget package, which is in my root library.

@user1121956 2012-12-02 17:05:51

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

@Matt123 2020-02-18 20:56:25

Where can I edit the name of assembly to change it to the right name?

@user1121956 2020-02-19 12:57:06

in the project file (.csproj) manually or by right-click on the project -> Properties

@Bay Sola 2020-01-13 20:36:22

Started having this problem after "downgrading" from VS 2019 Enterprise to VS 2019 Professional. Although the error was showing in the Error window, I could build the project without problems. Tried many solutions from this thread and others like equalizing target frameworks, delete and make the reference again, deleting .suo file, etc. What worked for me was simply deleting the project in my local repository and cloning it again from the remote repository.

@JustJohn 2019-06-28 21:32:34

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.

@MPJ567 2019-03-05 15:14:58

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!

@Janis S. 2019-01-07 14:13:46

I was working on VS 2017 community edition and had the same issue with CefSharp nuget packages.

Packages were downloaded and restored successfully, project could be built and run successfully - only the markup indicated that the namespaces were not recognized.

All I had to do was to open the References section and click on one of the yellow exclamation signs.

enter image description here

After a few seconds the markup errors went away.

enter image description here

@Qwertie 2019-09-03 20:28:56

I think you'll find this only works if the Properties panel is opened (right-click a problematic reference and choose Properties.) Now can anyone explain why this works?

@Albert221 2018-07-16 06:47:01

In my case, I had two project in a solution, I added a sub-namespace into the referenced project, but when building, I didn't notice that the referenced project build failed and it used the last successfully built version which didn't have this new namespace, so the error was correct, that it cannot be found, because it didn't exist The solution was obviously to fix errors in referenced project.

@Samih A 2018-06-20 17:09:28

I know this thread is old but anyway I'm sharing, I have to install all third part dependencies of the imported assembly - as the imported assembly wasn't included as Nuget package thus its dependencies were missing.

Hop this help :)

@ZerosAndOnes 2018-04-24 07:08:19

In my case, adding the dll as a reference gave the type or namespace name could not be found error. However, copying and pasting the dll file directly in bin folder resolved the error.

No idea why this worked.

@ksun 2013-05-21 17:02:52

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:

  1. I right-clicked on my Solution in the Solution Explorer and selected "Properties"
  2. Then in "Common Properties" I selected "Project Dependencies".
  3. Then in the Projects drop-down menu I selected the project that relied on the library, and
  4. Checked the box next to the library found under "Depends On"

This ensures that the library project gets built first.

@Kim 2013-11-05 21:25:36

Thanks for the tip to look at the warnings. My problem was that my test project needed to install the NuGet package for Bcl because my main project was referencing it.

@Chris Davis 2018-01-31 18:18:53

Thanks! This prompted me to find the issue I was having. Turns out I had two references to the dependency project, and the one that was taking precedence was a previously built DLL in the bin folder. I deleted the DLL and the rogue reference and did a rebuild, everything compiled correctly then.

@Robin Güldenpfennig 2017-12-07 12:20:17

To solve this issue it can also help to delete and recreate the *.sln.DotSettings file of the associated solution.

@Michael D. 2017-12-06 14:01:28

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:

  1. Unload the referenced project
  2. Open .proj file in VS ( i was looking for duplicates as someone suggested here)
  3. Reload project again (I did not change or even save the proj file as I did not have any duplicates)

@yossico 2017-11-13 07:51:50

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

@user1959309 2020-01-27 09:59:15

This was the exact issue for me. I removed System.Core and rebuilt, errors immediately resolved themselves. Thanks a million for saving me a lot of headache

@Jalal 2017-09-05 08:00:43

It event happen in Visual Studio 2017.

  1. Restart Visual Studio
  2. Clean project that fail to build.
  3. Rebuild the project.

@Simon_Weaver 2017-05-18 22:02:39

I got this error trying to make a build with a Visual Studio Team Services build running on my local machine as an agent.

It worked in my regular workspace just fine and I was able to open the SLN file within the agent folder locally and everything compiled ok.

The DLL in question was stored within the project as Lib/MyDLL.DLL and references with this in the csproj file:

<Reference Include="MYDLL, Version=2009.0.0.0, Culture=neutral, PublicKeyToken=b734e31dca085caa">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>Lib\MYDLL.dll</HintPath>
</Reference>

It turned out it literally just wasn't finding the file despite the hint path. I think maybe msbuild was looking relative to the SLN file instead of the project file.

In any case if the message you get is Could not resolve this reference. Could not locate the assembly then make sure that the DLL is in an accessible location to msbuild.

I kind of cheated and found a message that said Considered "Reference\bin\xxx.dll" and just copied the dlls into there instead.

@Andrey Kotov 2017-04-10 22:15:36

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.

@Cryptc 2019-10-16 15:02:13

I had a similar issue with a bad merge in VS 2019. The project compiled successfully, but not the solution. There was actually an error for that project (very strange). It was failing because of an extra file being referenced that had previously been removed, but then re-added from a merge. I removed the file, cleaned up the .csproj file, rebuilt, and all of the references started working again.

@Trevor 2017-04-03 20:32:18

In my case I went into the solution properties and made sure "Build" was checked in the Configuration for both projects. My main project didn't have it checked.

enter image description here

@Alexander Høst 2016-06-20 21:11:31

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.

@themefield 2018-08-10 23:44:17

Highly recommend this trick whenever finding any referencing issue within a VS solution. It solved my problem in VS2017 after I added in a new project targeting a higher version of .NET framework. I bet there is some caching cleared.

@mike 2018-09-24 21:55:30

Same here: this is what helped (I also didn't have the other version issues). I got error messages for every file that was open, up to 400+ ...although building/running was not a problem. Also: ReSharper's solution-wide-analysis also showed the same errors.

@Daniel Lerps 2019-01-24 07:51:43

Helped me too this trick. Didn't even have any target version differences. Building was possible, Rider didn't show any problems but VS kept on insisting that all project references were missing...

@Kev 2020-06-03 12:00:34

The compiler compiles according to the project build order, so if there is a genuine error in one project it can get lost in a sea of red-herring "type or namespace could not be found" errors - because it just exits when it finds the error, and doesn't get to updating the references. If there aren't too many errors listed, you should be able to find the genuine error. Unfortunately there were 100s of them for me, so this trick really helped me. I guess intelisense doesn't care aout the build order and just compiles projects individually, and that's why you don't get intelisense errors.

@Coding Enthusiast 2020-08-04 12:48:31

Same here with the twist that I had to remove 2 references and re-add them again for it to work. The project getting the error was Test and in the 2 references one was also referencing the other (library and UI).

@yu yang Jian 2016-06-15 03:18:30

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.

@Ilia Anastassov 2016-08-17 09:08:17

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.

@user3784340 2016-06-11 13:47:10

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

@Dan 2016-05-03 16:23:47

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

  1. Remove all nuget packages in my solution.
  2. Close and reopen my solution.
  3. Re-add all the nuget packages.

A little painful but was the only way I could get my website to publish to Azure.

@slugster 2010-07-22 00:22:10

This can be the result of a .Net framework version incompatibility between two projects.

It can happen in two ways:

  1. a client profile project referencing a full framework project; or
  2. 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)

@Greg 2010-07-22 01:06:57

excellent - this worked - I had to upgrade my WPF app client to use the full .NET Framework 4. Not sure what impact this will have on the client footprint? I did try downgrading the library I have to the .Net 4 Client Profile however when I did this it had similar issues with the recent Quartz.net 3rd party library I'd just started using. So it seems like using Quartz.net in my library project is ultimately forcing me to have to use the full .Net 4 framework in my UI WPF app.

@bong 2010-11-01 20:38:17

I had this same problem as well when I tried referencing a project within the same solution. I upgraded to .NET Framework 4 from the Client Profile framework and I stopped receiving the compile errors.

@Vitaliy Ulantikov 2011-10-18 12:30:48

Checked through my projects - all used 3.5 client profile, but I was still getting errors. Moving to 3.5 full was a helpful workaround, but your doe not explain this. Anyway, thumbs up!

@Richard 2012-09-04 19:25:16

Thanks - this helped just now. I recently moved the solution to VS2012 from VS2010, and had created one new class library in VS2012. All of a sudden I was getting this error, and of course it was because the new class library targeted .NET 4.5 while the project referencing it targeted .NET 4.0. Downgrading the new library to target 4.0 fixed it.

@Jason Coyne 2012-11-16 15:56:33

Really would be nice if Visual Studio would give you some sort of hint about this!

@Triynko 2013-09-27 19:42:37

This is a bizzarre error for sure. When I added the required DLL as a reference, added the using statements, and typed the class names in, they were highlighted in blue as expected and I could right click them and view their definition. Upon compiling, it returned the names to black text and complained that the namespace didn't exist. If I removed and re-added the reference, it would once again highlight everything, and then, once again, when I tried to compile it would complain. I'm surprised the project's target framework caused the DLL to be excluded entirely without warning. Terrible.

@TruthOf42 2013-10-02 13:54:23

I changed the target of my program using the offending reference from 3.5 to 4.0 and all is dandy now!

@Jon Story 2015-10-14 15:00:46

Although this answer gives a great description of what needs doing... it has no suggestion on how to do it, which would be a nice addition

@slugster 2015-10-14 22:27:41

@JonStory Fair enough comment, I assumed that everyone would know how to change the framework that their project is referencing. I'll add some pictures in the next day or so to illustrate that part of it.

@Jon Story 2015-10-14 22:46:58

Even the best of us have sometimes just never had the need to do some tasks. I'm not sure how I've never needed to change the framework and although I have now found it, it hadn't occurred to me before. It's not a deal breaker for the answer, just that I find the very best answers act as a one stop shop for 'describe the problem, state the solution, show how to fix it'

@Osprey 2016-02-15 14:07:57

I am having this problem but cannot figure out how to "upgrade the framework target". The site works fine when running on my local server but gives me this error when running on the live server. I copied the website folder exactly to the live server. Both instances have the same web.config with <compilation debug="true" targetFramework="4.0" />. I don't "build" a website. I just create the pages in VS2010 and upload the files exactly as they are on my local machine and they usually work. I have other sites on the same live servers and only this one is being problematic.

@slugster 2016-02-16 00:26:45

@Osprey your's sounds like a different issue - the issue described above happens at development time. You might be better off starting a new question with your exact issue and error message/logging.

@Osprey 2016-02-16 09:19:38

@slugster . You are right. It turns out that the problem was that I was trying to run the site from a sub-folder in the domain name root and therefore it was not finding the App_Code folder. I moved it to the domain root and it worked.

@slugster 2016-02-16 09:51:43

@Osprey It's great when you solve the issue, although you'll kick yourself sometimes :)

@Al Lelopath 2016-05-03 19:38:41

How do I determine if the projects are using the Client Profile or Full .Net? When I look on Project Properties > Application> Target framework, it says .NET Framework 4, with no indication of Client Profile or Full.

@Felix Aballi 2016-05-26 14:50:14

Guidance: 1) assembly loaded?, 2) assembly loaded matches with origin assembly?, 3) "using" directives pointing to old or none valid references?, 4) .csproj manifest includes source invalid?, 5) a search tool looking regex in the entire solution (every class library and project). 5) check project settings for net framework version build option (collaborate in teams bring on this kind of problen, you must agree net framew. build version in both sides) 6) After that clean and build each by separate and finally, include all references to the destination project/class library. I should work!

@Yushatak 2016-08-15 17:06:34

Why is it creating new projects at an arbitrary .NET framework of 4 rather than the latest it can create? I could understand having a default, but why that? -_-

@Prashant Pimpale 2019-09-18 11:02:07

@slugster I have only one projects and getting this error: target framework is 4.0 and VS 2015

@Nick Gotch 2015-09-09 16:07:38

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.

@Tanuki 2015-06-18 11:58:31

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.

@djangojazz 2014-09-04 22:38:37

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.

@saxorut 2015-05-26 15:42:13

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

@Ádám Kovács 2019-01-20 15:33:33

The problem I faced was that I created a new solution, that I added a different version of a Nuget package, than others are used. Then, when I ran Update-Package -reinstall I had several errors in all of the solution, that included a different version of this package. I updated in all then it finally run through. It fixed also the references in the package.json files from 45 to 452, since I also changed the target version time ago.

@Binil Anto 2019-08-27 09:13:38

Worked for me and I had to remove Sytems.Net.Http from references while downgrading

@foremaro 2019-09-25 19:46:28

This was also what worked for me. Ran the command, then un-did the resulting pending changes and my sln was back to normal

@Adleri 2020-01-16 19:02:02

It worked for me, it showed me a reference that wasnt supported with my current framework

@CAD bloke 2020-05-04 11:20:56

this worked for me and the error that went away was totally unrelated to any nuget package installed.

@Kim 2013-12-11 15:34:49

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.

@Nicola Iarocci 2014-10-29 09:33:06

Ah thanks for this. My portable project was compiling fine on Xamarin Studio while it would fail on Visual Studio because of this. I think XS does some 'magic' to let it compile when implicit references are missing.

@Bill 2013-07-21 12:39:24

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.

Related Questions

Sponsored Content

88 Answered Questions

[SOLVED] Metadata file '.dll' could not be found

27 Answered Questions

[SOLVED] Why not inherit from List<T>?

19 Answered Questions

[SOLVED] Namespace not recognized (even though it is there)

  • 2010-11-19 20:13:25
  • Vaccano
  • 182444 View
  • 146 Score
  • 19 Answer
  • Tags:   c# reference

26 Answered Questions

[SOLVED] Error message "No exports were found that match the constraint contract name"

  • 2013-07-11 14:47:24
  • Simon B.Robert
  • 323093 View
  • 1071 Score
  • 26 Answer
  • Tags:   visual-studio

19 Answered Questions

[SOLVED] How do I fix the Visual Studio compile error, "mismatch between processor architecture"?

  • 2012-04-11 20:35:25
  • Paul Eastlund
  • 358607 View
  • 462 Score
  • 19 Answer
  • Tags:   .net visual-studio

2 Answered Questions

[SOLVED] The type or namespace name could not be found, Beginner in C#

Sponsored Content