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

Ran*_*Rag 31 c# namespaces

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

现在我已将上面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");

            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

现在当我尝试使用csc test.cs我编译我的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?)
Run Code Online (Sandbox Code Playgroud)

那么,我在这里做错了什么,因为根据我的说法,我不会错过任何一个using directive.

小智 28

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


Jon*_*eet 15

这就是问题:

C:\Users\Noob\csharp>csc test.cs
Run Code Online (Sandbox Code Playgroud)

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

C:\Users\Noob\csharp>csc test.cs /r:SnarlNetwork.dll
Run Code Online (Sandbox Code Playgroud)

(或任何组件被称为).

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

C:\Users\Noob\csharp>csc test.cs SnarlNetwork.cs
Run Code Online (Sandbox Code Playgroud)

如果您还没有编译程序集但想要,可以使用:

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

csc Test.cs /r:SnarlNetwork.dll
Run Code Online (Sandbox Code Playgroud)

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


小智 9

我通过在项目应用程序设置中更新目标框架来解决此问题.该项目正在使用.net框架版本4.5.1进行编译,但引用的dll是使用版本4.6.1编译的.所以更新了我的项目版本.在此输入图像描述


Jam*_*ood 7

编辑:哦,不理我,你不是在使用Visual Studio.

您是否添加了对项目的引用?

就像在这种事情:

在此输入图像描述

  • 对于我们不太熟悉Visual Studio的人来说,这仍然是一个有用的答案. (2认同)
  • 如何在没有 Visual Studio 的情况下手动添加引用?我必须更改什么文本文件? (2认同)