开发者

namespace not found!

开发者 https://www.devze.com 2023-03-20 06:00 出处:网络
I created a solution called Foo. Added a class library called Foo.Common Added a console app to call the library code from called ConsoleApp.

I created a solution called Foo. Added a class library called Foo.Common Added a console app to call the library code from called ConsoleApp.

I referenced the Foo.Common from ConsoleApp and typed :

using Foo.Common;
public class Program
{
    CommonClass c = new CommonClass();            

    static void Main(string[] args)
    {
 开发者_如何学Python   }
}

and get this back :

Error 1 The type or namespace name '**Foo**' could not be found (are you missing a using directive or an assembly reference?) Z:\Foo\Solution1\ConsoleApplication1\Program.cs 3 11 ConsoleApplication1

Why am i getting this?

what s going on?


Make sure that

  • The ConsoleApp project has a reference to the Foo.Common project (do not browse for Foo.Common.dll),

    namespace not found!

  • the file contains a using directive for the namespace in which CommonClass is declared, and

  • CommonClass is declared as public.

So your files should look like this:


CommonClass.cs in Foo.Common project:

namespace Foo.Common
{
    public class CommonClass
    {
        public CommonClass()
        {
        }
    }
}

Program.cs in ConsoleApp project:

using Foo.Common;

namespace ConsoleApp
{
    public class Program
    {
        public static void Main()
        {
            CommonClass x = new CommonClass();
        }
    }
}


Ensure that under your project settings, the target framework is set as .NET Framework 4 and not .NET Framework 4 Client Profile. I got this same behavior when it was set to Client Profile and it was fixes as soon as I set it to just the regular .NET Framework 4.


Right Click on the new console app solution/project and Add Reference and add the project that contains the Foo namespace


Did you add a reference to the library? Look under "References" in the console project. If its not there, you need to add it.


I posted this as a comment, but I want to expand on it here. What's probably happening is it's seeing using as a statement and not a keyword. It appears you have something like the following:

using System;
namespace TestNamespace
{
  using Foo.Common;
  public Class { }
}

Try

using System;
using Foo.Common;
namespace TestNamespace
{
  public Class { } 
}

Instead.


It looks like Foo Bar got this error because his project's target framework was set to the client profile.

Just thought I'd add one more 'solution' -- I created a library that targeted the 4.5 framework. My older project was tarting the 4 framework. I got this error.

Changing the older project to 4.5 made it work.

0

精彩评论

暂无评论...
验证码 换一张
取 消