Predefined type 'System.Object' is not defined or imported .net 4.6

C#asp.net.NetJsonasp.net Mvc

C# Problem Overview


I'm using MVC 5 and .net 4.6 to create a web API that my mobile app can connect too.

Whenever I build the project I get hundreds of errors telling me that I need to add references that are already there and that literally every type in my project 'does not exist'. Some examples of these errors: (Not including the one in the title)

ErrorList

What I've tried:

  • Added a reference to System.Runtime via nuGet. (A recommendation from this question.)
  • Removed aspnetcore50 from the frameworks in the project.json file. (Another recommendation, this question.)
  • Restarting VS2015, my machine.
  • Going through the nuGet package manager and upgrading every package in my project to the latest.
  • Actually making sure I can see the references in the references node of the solution explorer.

UPDATE: -My project.json file looks like this. (After modifying based on Karls answer. The error list is now down to ten, however they are still the same errors as before.)

{
  "commands": {
    "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000"
  },
  "dependencies": {
    "Microsoft.AspNet.Server.IIS": "1.0.0-beta5",
    "Microsoft.AspNet.Server.WebListener": "1.0.0-beta5",
    "Microsoft.AspNet.Diagnostics": "1.0.0-beta5",
    "Microsoft.AspNet.Mvc": "6.0.0-beta5",
    "System.Runtime": "4.0.20-beta-23019"
  },
  "exclude": [
    "wwwroot",
    "node_modules",
    "bower_components"
  ],
  "frameworks": {
    "dnx451": {
      "frameworkAssemblies": {
        "System.Data": "4.0.0.0",
        "System.Data.DataSetExtensions": "4.0.0.0",
        "System.Data.Linq": "4.0.0.0"
      }
    }
  },
  "publishExclude": [
    "node_modules",
    "bower_components",
    "**.xproj",
    "**.user",
    "**.vspscc"
  ],
  "version": "1.0.0-*",
  "webroot": "wwwroot"
}

C# Solutions


Solution 1 - C#

Remove bin and obj and rebuild.

Solution 2 - C#

This wasted lot of my time. Close the visual studio. Delete the .vs folder and restart visual studio. Everything should work fine.

Usually this happens when you change branches or merge branches.

Solution 3 - C#

I run dotnet restore command on Package manager console and problem solved.

Solution 4 - C#

I had the same problem. I spent two days trying to resolve this problem. I tried reinstalling Visual Studio, cleared Visual Studio cache, uninstalled Visual Studio or setup Windows 10 again => but with no success.

You can try follow steps which worked for me:

  1. Close Visual Studio
  2. Delete the packages folder in Solution folder
  3. Open Visual Studio and rebuild solution

Good luck

Solution 5 - C#

I just opened Visual Studio "As Administrator" and it worked!

Solution 6 - C#

I'm guessing a bit here but it looks suspicious that you are using the regular System.Data.* stuff for dnx451 but doesn't have anything to make up for it for dnxcore50. Are you targeting the core edition as well or is it just leftovers from project creation? If you want to target the core edition and still use the old System.Data.* stuff you'll need to wrap anything from those assemblies like this.

#if DNX451
// Do some DNX451 specific stuff here
#endif

And the other way around as well.

#if DNXCORE50
// Do some DNXCORE50 specific stuff here
#endif

But if you aren't thinking about supporting this yet I would simply remove dnxcore50 from the framework list and your errors will probably go away.

Solution 7 - C#

In my case, I had a project that was still set to build against a beta version of dnx. Changing it to build against the version I was using in the other projects fixed it.

Solution 8 - C#

In my case, below are the steps I followed;

  1. I removed bin and obj and rebuild. (Didn't work)
  2. Unloaded the project. (It resulted in "Assets file project.assets.json not found. Run a NuGet package restore")
  3. I ran dotnet restore command via Visual Studio 2017 >> Tools >> NuGet Package Manager >> Package Manager Console.
  4. Rebuild solution. (Problem solved)

Solution 9 - C#

Took almost 2 days to fix. Tried VS Uninstall\re-install several times but did not work.

What finally worked for me...

Ran VS Installer repair. Due to errors, I had to manually uninstall all instances MS .NET Core SDK - 2.1xxx

Ran VS Installer repair. No errors. Remove bin and obj and rebuild.

Ran dotnet restore command on Package manager console and problem solved.

Solution 10 - C#

VS2019 solution files were placed under the One drive folder, moving to a different folder outside one drive, fixed this.

Solution 11 - C#

For me, the problem occurred when I added a new project to my solution. I re-built the solution and the errors went away.

Solution 12 - C#

In my case, this is different. First check System.Runtime reference is added to your project or not. If it is added to your project, please follow the below steps.

  1. Open your Reference from your project and select System.Runtime assembly.
  2. Right click on System.Runtime and select property.
  3. Change the value of SpecificVersion from False to True.

Note : In my Case I am using Visual Studio 2019 and Project Framework Version - 4.6.2

Solution 13 - C#

I too received this problem using Visual Studio 2019 and loading a Solution from Google/Git. When my solution was loading, Output tab (from Package Manager), stated that the folder didn't exist: google-api-dotnet-client\NuPkgs\Support. I added the folder NuPkgs then Support within NuPckgs and restarted Visual Studio then all of the projects restored. This fixed my problem.

Check the output tab (from Package Manager) to ensure your projects are restoring successfully.

Solution 14 - C#

Do these errors only appear when a view (.cshtml) file is open and go away when the file is closed or tabbed away?

MVC5 defines global references for .cshtml razor files to assume at for all views. Check here: project -> Views -> web.config (not to be confused with the project -> web.config file. The system.web.webPages.razor section defines the references:

<configuration>
  ...
  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc,
        Version=5.2.7.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization"/>
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>
  ...

However if this project -> views -> web.config file is missing this may be by design, or is not required. I have a projects that when deployed its embedded into a larger top level site which defines these references and so during runtime these errors are cleared. I've learned to ignore and just close the .cshtml files or select non .cshtml file.

It may be possible to grab a copy of the top level views -> web.config file and add it to your solution. I'd think when published the file will be ignored but I am not sure about this behavior.

Solution 15 - C#

My situation: I am developing in VS 2019 using .NET Framework 4.5. I addded a class library and got the Predefined type 'System.Object' is not defined message on the generated class.

Efforts:

  • Deleted the bin and obj folders, and rebuilt. No change.
  • Ran dotnet restore in the package manager console. Run completed, saying there was nothing for it to do. No change.

My solution: I unloaded the project and reloaded with dependencies. When I rebuilt, the errors went away.

Solution 16 - C#

if it helps anybody:

my problem was that the compiler couldn't find System.Index (or something like that)

the error-message didn't really help that much, but then I noticed array[^1] which after some search turns out is just a fancy way to index from the end of the array.

simply changing array[^1] to array[array.Length-1] did the trick.

not the most elegant solution but maybe it helps

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKidCodeView Question on Stackoverflow
Solution 1 - C#gnericView Answer on Stackoverflow
Solution 2 - C#Tharindu JayasingheView Answer on Stackoverflow
Solution 3 - C#Navid_pdp11View Answer on Stackoverflow
Solution 4 - C#Bình Nguyễn QuangView Answer on Stackoverflow
Solution 5 - C#FarkhodView Answer on Stackoverflow
Solution 6 - C#Karl-Johan SjögrenView Answer on Stackoverflow
Solution 7 - C#PeterView Answer on Stackoverflow
Solution 8 - C#BloggrammerView Answer on Stackoverflow
Solution 9 - C#James RobertsView Answer on Stackoverflow
Solution 10 - C#TheLoneWolf91193View Answer on Stackoverflow
Solution 11 - C#Sarah WesselView Answer on Stackoverflow
Solution 12 - C#shailesh MView Answer on Stackoverflow
Solution 13 - C#Lloyd S TurleyView Answer on Stackoverflow
Solution 14 - C#Russ EbbingView Answer on Stackoverflow
Solution 15 - C#S. RojakView Answer on Stackoverflow
Solution 16 - C#AntonView Answer on Stackoverflow