0

我只是尝试使用带有演示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 框架也很陌生。

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

4

1 回答 1

1

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

2:
mcs是 Mono C# 编译器,与 .NET 无关。而是使用dotnet cli运行恢复构建等等。


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

  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. 这将编译并运行您的项目。

PS:我强烈建议遵循C# Programming with Visual Studio Code guide中给出的建议

于 2020-10-09T08:16:26.500 回答