C# 为什么我收到错误 CS0246:找不到类型或命名空间名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11854308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:31:04  来源:igfitidea点击:

Why am I getting error CS0246: The type or namespace name could not be found?

c#namespaces

提问by RanRag

I am using Snarl C# APIto send notifications to snarl.

我正在使用Snarl C# API向 snarl 发送通知。

Now I have saved the content of above url in a file named SnarlNetwork.csand the content of my test.csfile are:

现在我已经将上面 url 的内容保存在一个名为SnarlNetwork.cstest.cs文件中,我的文件内容是:

using SnarlNetworkProtocol;
using System;
class test
{
    public static void Main(String[] args)
    {
        SNP snarl_object = new SNP();
        string hostname = "localhost";
        string hostport = "9887";
        string appName = "Spotify";

        bool val = snarl_object.register(hostname, hostport, appName);

        if (val == true)
        {
            string title = "hello";
            string message = "world";
            string timeout = "5";
            bool newval = snarl_object.notify(hostname, hostport, appName, null, title, message, timeout);

            if (newval == true)
            {
                Console.WriteLine("sucessfull");

            }
        }
    }

}

Now when I try to compile my test.cs file using csc test.csI get the following error:

现在,当我尝试使用编译我的 test.cs 文件时csc test.cs,出现以下错误:

C:\Users\Noob\csharp>csc test.cs
Microsoft (R) Visual C# 2008 Compiler version 3.5.30729.4926
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.

test.cs(1,7): error CS0246: The type or namespace name 'SnarlNetworkProtocol' could not be found (are you missing a using directive or an assembly reference?)

So, what am I doing wrong here because according to me I am not missing any using directive.

所以,我在这里做错了什么,因为据我说我没有错过任何using directive.

采纳答案by Jon Skeet

This is the problem:

这就是问题:

C:\Users\Noob\csharp>csc test.cs

You haven't added a reference to the DLL. You need something like:

您尚未添加对 DLL 的引用。你需要这样的东西:

C:\Users\Noob\csharp>csc test.cs /r:SnarlNetwork.dll

(or whatever the assembly is called).

(或任何程序集的名称)。

Alternatively, if you haven't got it as a separate library, just compile both files:

或者,如果您还没有将其作为单独的库,只需编译这两个文件:

C:\Users\Noob\csharp>csc test.cs SnarlNetwork.cs

If you haven't compiled an assembly but wantto, you can use:

如果您尚未编译程序集但编译,则可以使用:

csc /target:library /out:SnarlNetwork.dll SnarlNetwork.cs

csc Test.cs /r:SnarlNetwork.dll

(In fact, specifying the output file is unnecessary in this particular case, but it's still clearer...)

(实际上,在这种特殊情况下不需要指定输出文件,但它仍然更清晰......)

回答by James Wood

Edit: Oh ignore me, you're not using Visual Studio.

编辑:哦忽略我,你没有使用 Visual Studio。

Have you added the reference to your project?

您是否已将引用添加到您的项目中?

As in this sort of thing:

就像在这种事情中:

enter image description here

在此处输入图片说明

回答by Sam Axe

This usually happens to me when I have a using statement but have forgotten to reference the assembly that defines the namespace.

当我有一个 using 语句但忘记引用定义命名空间的程序集时,这通常会发生在我身上。

But in your case, as the namespace is defined in a file in your project, you have forgotten to tell the compiler about the snarlnetwork.cs file.

但是在您的情况下,由于命名空间是在项目的文件中定义的,您忘记告诉编译器有关 snarlnetwork.cs 文件的信息。

See csc compiler examples

请参阅csc 编译器示例

回答by wely.lau

It might be due to "client profile" of the .NET Framework. Try to use the "full version" of .NET.

这可能是由于 .NET Framework 的“客户端配置文件”。尝试使用 .NET 的“完整版”。

回答by Stefan Michev

I had this error on a MVC Project. And after a long research i found out that the .cs file containing some of the classes i referenced in the main project had a Build Actions set to "Content" ...

我在 MVC 项目上遇到了这个错误。经过长时间的研究,我发现包含我在主项目中引用的一些类的 .cs 文件的构建操作设置为“内容”......

After changing it "Content"->"Compile" the error disappeared.

更改“内容”->“编译”后,错误消失了。

回答by AdvanTiSS

I had the same error when used cs file was located inside project folder, but did not referenced from .csproj of parent project. Intellisence see this file inside project folder, but compiler does not see it because of missing reference in .csproj

当使用的 cs 文件位于项目文件夹中时,我遇到了同样的错误,但没有从父项目的 .csproj 引用。Intellisence 在项目文件夹中看到此文件,但由于 .csproj 中缺少引用,编译器没有看到它

回答by maxspan

I have resolved this problem by adding the reference to System.Web.

我通过添加对 System.Web 的引用解决了这个问题。

回答by Vishwa G

  1. On the Solution Explorertab right click and select Properties

  2. Resolve this issue by updating the Target Frameworkin the project application settings.

  1. 解决方案资源管理器选项卡上右键单击并选择属性

  2. 通过更新项目应用程序设置中的目标框架来解决此问题。

For instance, In my case the project was compiling with .net framework version 4.5.1 but the dll which were referenced were compiled with the version 4.6.1. So have updated the my project version. I hope it works for you.

例如,在我的情况下,项目使用 .net 框架版本 4.5.1 编译,但引用的 dll 使用版本 4.6.1 编译。所以更新了我的项目版本。我希望这个对你有用。

enter image description here

在此处输入图片说明

回答by Actek14

I was using .NET Framework 4.5but my new library had .NET Framework 4.5.2and I got the same issue when I tried to build. I solved it by updating my project from 4.5to 4.5.2(same as my library).

我使用的是.NET Framework 4.5,但我的新库有.NET Framework 4.5.2,我在尝试构建时遇到了同样的问题。我通过将我的项目从4.5更新到4.5.2(与我的库相同)来解决它。

回答by Yahoo Serious

I also got this error due to a missing reference. The reason I did not notice is because Resharper offers to add a using and a reference. Adding the using succeeds (but it's highlighted grey), syntax highlighting of missing classes works (sometimes), but adding the reference fails silently.

由于缺少参考,我也遇到了这个错误。我没有注意到的原因是因为 Resharper 提供添加使用和引用。添加 using 成功(但它以灰色突出显示),缺少类的语法突出显示(有时)有效,但添加引用以静默方式失败。

When manually adding the reference an error pops up, explaining why adding the reference fails (circular reference). Resharper did not pass this error on to the GUI.

手动添加引用时会弹出错误,解释添加引用失败的原因(循环引用)。Resharper 没有将此错误传递给 GUI。