【问题标题】:The type or namespace name "PeterO" could not be found [closed]找不到类型或命名空间名称“PeterO”[关闭]
【发布时间】:2020-10-09 07:50:29
【问题描述】:

我只是尝试在演示 hello.cs 程序中使用https://github.com/peteroupc/CBOR repo。但我最终会遇到以下问题。

1) 属性 PeterO.Cbor 没有像其他属性一样反映颜色。请参考以下代码。

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using PeterO;
using PeterO.Cbor;  // Why this attribute is in different color?

namespace Hello
{
    public class HelloWorld
    {
        public static void Main(string[] args)
        {
            Console.WriteLine ("Hello World!");
            var cbor1 = CBORObject.NewMap()
               .Add("item", "any string")
               .Add("number", 42)
               .Add("map", CBORObject.NewMap().Add("number", 42))
               .Add("array", CBORObject.NewArray().Add(999f).Add("xyz"))
               .Add("bytes", new byte[] { 0, 1, 2 });
            // The following converts the map to CBOR
            byte[] bytes = cbor1.EncodeToBytes();
            // The following converts the map to JSON
            string json = cbor1.ToJSONString();
            System.Console.WriteLine(json);
            Console.ReadKey();
        }
    }
}

2) hello.cs 程序执行时出错。请从下面的快照中找到相同的内容。

使用的命令:mcs hello.cs -out:hello

编译错误输出消息:

hello.cs(8,7): error CS0246: The type or namespace name `PeterO' could not be found. Are you missing an assembly reference?
Compilation failed: 1 error(s), 0 warnings

其他信息:

1) Dotnet-sdk 和 Visual Studio Code 安装步骤如下:

Dotnet-sdk installation steps:
    a) wget https://download.visualstudio.microsoft.com/download/pr/f01e3d97-c1c3-4635-bc77-0c893be36820/6ec6acabc22468c6cc68b61625b14a7d/dotnet-sdk-3.1.402-linux-x64.tar.gz
    b) mkdir -p $HOME/dotnet && tar zxf dotnet-sdk-3.1.402-linux-x64.tar.gz -C $HOME/dotnet
    c) export DOTNET_ROOT=$HOME/dotnet
    d) export PATH=$PATH:$HOME/dotnet

Visual Studio Code installation steps:
    a) wget https://az764295.vo.msecnd.net/stable/2af051012b66169dde0c4dfae3f5ef48f787ff69/code_1.49.3-1601661857_amd64.deb
    b) sudo dpkg -i code_1.49.3-1601661857_amd64.deb && sudo apt-get update

2) Ubuntu、Dotnet 和 Visual Studio Code 版本详情:

a) Ubuntu Command: lsb_release -a

Output:
    No LSB modules are available.
    Distributor ID: Ubuntu
    Description:    Ubuntu 18.04.5 LTS
    Release:    18.04
    Codename:   bionic
b) Dotnet Command: dotnet --info

Output:
    .NET Core SDK (reflecting any global.json):
     Version:   3.1.402
     Commit:    9b5de826fd

    Runtime Environment:
     OS Name:     ubuntu
     OS Version:  18.04
     OS Platform: Linux
     RID:         ubuntu.18.04-x64
     Base Path:   /home/$USER/dotnet/sdk/3.1.402/

    Host (useful for support):
      Version: 3.1.8
      Commit:  9c1330dedd

    .NET Core SDKs installed:
      3.1.402 [/home/$USER/dotnet/sdk]

    .NET Core runtimes installed:
      Microsoft.AspNetCore.App 3.1.8 [/home/$USER/dotnet/shared/Microsoft.AspNetCore.App]
      Microsoft.NETCore.App 3.1.8 [/home/$USER/dotnet/shared/Microsoft.NETCore.App]

    To install additional .NET Core runtimes or SDKs:
      https://aka.ms/dotnet-download
c) Visual Studio Code details:

    Version: 1.49.3
    Commit: 2af051012b66169dde0c4dfae3f5ef48f787ff69
    Date: 2020-10-02T17:56:11.027Z
    Electron: 9.2.1
    Chrome: 83.0.4103.122
    Node.js: 12.14.1
    V8: 8.3.110.13-electron.0
    OS: Linux x64 5.4.0-48-generic

3) dotnet restore 命令输出:

Path: /path/to/repo/CBOR/
Command: dotnet restore
Output: 
    Determining projects to restore...
    /path/to/repo/CBOR/CBOR.csproj : error NU1108: Cycle detected.  [/path/to/repo/CBOR.sln]
    /path/to/repo/CBOR/CBOR.csproj : error NU1108:   PeterO.Cbor -> PeterO.Cbor (>= 4.2.0). [/path/to/repo/CBOR.sln]

因此,我将文件 /path/to/repo/CBOR/CBOR.csproj 中的 PackageId PeterO.Cbor 替换为 PackageId Peter Cbor。然后它给出了如下的构建输出。

Path: /path/to/repo/CBOR/
Command: dotnet restore
Output:
    Determining projects to restore...  Restored 
    /path/to/repo/CBOR/CBOR.csproj (in 306 ms).
    Restored /path/to/repo/CBORTest/CBORTest.csproj (in 319 ms).
    6 of 8 projects are up-to-date for restore.

我对 Visual Studio 代码和 dotnet 框架也很陌生。

谁能在这个问题上纠正我,让我知道我哪里出错了。

【问题讨论】:

  • 您是否真的在项目中添加了对Peter0 的引用?您的参考资料的屏幕截图可能会有用。
  • 它被称为“using指令”。 C# 有一个完全不同的特性,称为attribute,所以最好不要混淆名称。
  • @mjwills 我已经使用 nuget 数据包管理器安装了 PeterO.Cbor 包。仅安装后,我最终遇到了上述错误。
  • @WSC 您能否参考链接中的所有参考点github.com/peteroupc/CBOR/blob/master/CBOR/CBOR.csproj

标签: c# .net visual-studio-code cbor


【解决方案1】:

1:
这不是一个属性,它被称为 using directive,它与 Java、Python(可能更多)中的 import 基本相同,并且与 C/C++ 中的 #include 相当。 Attributes 在 C# 中是完全不同的东西。

2:
mcs 是 Mono C# 编译器,与 .NET 无关。而是使用dotnet clirunrestorebuild 等等。


现在,您的整个问题有点令人困惑,所以我将逐步向您展示如何做您想做的事情。

  1. 创建一个新项目
dotnet new console --name YourProjectName

这将创建一个新目录YourProjectName 并将您需要的所有内容放入其中。即YourProjectName.csproj文件和Program.cs文件。 Program.cs 只是一个约定,您的入口点(Main 方法)可以位于任何地方。

  1. 添加 CBOR 包
    CBOR 在NuGet 上可用,这意味着我们可以简单地运行
dotnet add package PeterO.Cbor --version 4.2.0

这将完成我们需要它做的所有事情。在引擎盖下,这将添加

<ItemGroup>
  <PackageReference Include="PeterO.Cbor" Version="4.2.0" />
</ItemGroup>

到您的 YourProjectName.csproj 文件并恢复(下载/构建任何依赖项)您的项目。

  1. 现在编辑Program.cs 现在你可以打开 VS Code(或任何文本编辑器)并编辑 Program.cs 来做你想做的事。如果你想测试你的程序,只需切换到终端,cd 到你的项目目录并运行dotnet run。这将编译并运行您的项目。

P.S:我强烈建议遵循C# programming with Visual Studio Code 指南中给出的建议

【讨论】:

  • 创建一个新项目并使用 nuget cli 安装 cbor 工作正常。谢谢。 dotnet new console --name YourProjectName; dotnet add package PeterO.Cbor --version 4.2.0; dotnet restore; dotnet run; 但是,我想将 cbor 与 fiddler 工具集成,它与单声道一起使用。我该怎么做?
  • 如果我尝试运行命令mcs Program.cs -out:exe,我会收到Program.cs(2,7): error CS0246: The type or namespace name `PeterO' could not be found. Are you missing an assembly reference? Compilation failed: 1 error(s), 0 warnings的错误
  • 好吧,那么您需要使用 mono 而不是 dotnet 来构建您的应用程序。但这很容易,因为您可以简单地使用msbuild 命令来构建您的.csproj 项目。如果您想将其用作提琴手扩展程序,您需要遵循their 指南。
  • 不要直接使用mcs,使用构建系统(msbuild),否则你需要自己手动指定所有依赖和一切,类似于必须指定所有链接器构建 C/C++ 项目时手动设置参数
猜你喜欢
  • 2011-08-10
  • 1970-01-01
  • 1970-01-01
  • 2017-07-12
  • 2011-05-13
  • 2013-03-25
  • 2012-06-19
  • 2017-11-29
相关资源
最近更新 更多