44

I've got a Solution with lots of projects and all but one of them is behaving. The one that is not working is a ConsoleApplication, and it relies on C# Class Library project. I've added a reference to the library project, and add the namespace (which I've checked is correct), but everywhere I reference the classes in my library, I get:

The type or namespace 'MyClass' could not be found (are you missing a using directive or an assembly reference?).

The library project is building successfully (I can see the DLLs appear in the bin folder) and I've tried a project reference, and also a reference to the DLL itself. Neither works.

Also, all projects are set to build with a platform target of 'Any CPU'.

I've tried pretty much every suggestion I've come across on forums with no success. Can anyone shed some light on what's going wrong?

Thanks

This solved the problem:

The console application had a Target framework of .NET Framework 4 Client Profile, whereas the library just had .NET Framework 4. I set the console app to .NET Framework 4 and it all builds perfectly.

Daniel Dušek
  • 13,683
  • 5
  • 36
  • 51
Richard
  • 1,731
  • 2
  • 23
  • 54

6 Answers6

98

My bet is on a framework mismatch between your library and you app... Check if your library is not building with a superior version than you app, or if your app is building with a Client profile flavor

Cédric Rup
  • 15,468
  • 3
  • 39
  • 30
  • Yep, you were spot on, I've put a full answer below - thanks for the tips Cédric – Richard Mar 20 '12 at 15:57
  • 7
    Thanks everyone, that was quick! :) Yes, the console application had a Target framework of .NET Framework 4 Client Profile, whereas the library just had .NET Framework 4. I set the console app to ".NET Framework 4" and it all builds perfectly. I wouldn't have thought to look there given the wording of the error so thanks for the tips. I guess it's one of those mistakes you only make once and remember it for next time (...?) Thanks! – Richard Mar 20 '12 at 16:01
  • Oh my, i though i would spend a whole day trying to solve this, and then there is you :D – Crystian Leão Feb 28 '14 at 03:46
  • Thanks a lot, that was exactly the problem. I wished there was a more useful error when building indicating this problem... Gladly I found this post before losing a lot of time on it. Thanks! – Tom Aug 01 '14 at 09:32
  • For the record, because it worked for me: be sure you are typing the namespace of the library you're using, not the name of the class. – Alejandro Bastidas Apr 21 '15 at 15:02
  • Exactly! For my case, it was that one project(build with .net 4.5.2) was referencing another one(build with .net 4.6.2) which had a superior .NET version. – Arsen Khachaturyan Apr 16 '18 at 07:37
  • Thanks a lot!!! I had 0 Errors but Build failed! And I was like.. "Wait! What!?" o.O – Gabriel G Apr 21 '18 at 02:15
  • Thank you! I was struggling with this between .net 5 and .netstandard 2.0....net does not support it. Still gotta figure out how to install .netstandard2.1. – mattylantz Nov 09 '21 at 06:33
3

It is probably that one of your DLLs references some part of the .net framework that is not referenced in your console application. For example if one of your class library projects has asp.net server controls in and references System.Web, but your console application does not reference System.Web it will not build and you will get that error. But it is not obvious because the DLLs referenced are stored in the GAC so they would never appear in your bin folder.

Ben Robinson
  • 21,601
  • 5
  • 62
  • 79
2

I had to simply restart visual studio for reference to work but make sure you have reference added in .csproj file.

If you still experience the issue, make sure the class you're referencing is public and that Asp.net core Framework version match.

Amir Dora.
  • 2,831
  • 4
  • 40
  • 61
1

Sounds weird,

Have you tried to remove the reference of the project and add it again? Check if your console app has got all the right references.

You could also inspect the .csproj file and see if everything is correct in there.

antao
  • 767
  • 1
  • 9
  • 24
0

Just Check that you "Class Library" project has classes in it or if it is a data access layer project which include only a .edmx Model check the Model designer is found and it generates fine.

Good Luck

DigitalFox
  • 1,486
  • 1
  • 13
  • 17
0

I worked with syncing the framework, but still, it was giving issue. So I tried another way. Right-click on the dependency, and select Add project reference. I added the required project then the error was gone.