ETH官方钱包

前往
大廳
主題

在CentOS 7上使用Visual Studio Code開發CSharp程式

Yang | 2023-01-25 07:38:32 | 巴幣 0 | 人氣 309

接續之前寫的:
1. 在CentOS 7使用CSharp開發程式的前置條件(安裝.NET 7)
2. 在CentOS 7上使用Visual Studio Code開發標準C語言程式
這裡紀錄在CentOS 7上使用VSCode 1.74開發CSharp程式

Step1:影片00:01,啟動VSCode,安裝輔助外掛工具,搜尋CSharp即可看到

Step2:影片00:38,建立專案資料夾/CSharp/UnitTest001

Step3:影片01:33,在專案資料夾內建立.vscode資料夾,預設是個隱藏資料夾,檔案總管看不到

Step4:影片02:30,指令ll -a或ls -a可以看到隱藏資料夾

Step5:影片09:30,在專案資料夾內輸入dotnet new console --use-program-main,建立不使用最上層語句(top-level statements)且具有Main方法的主控臺專案
(這裡先以舊式的CSharp寫法當範例,如果不帶參數--use-program-main,會使用.NET 6開始的新風格,最上層語句)

Step6:影片12:19,把UnitTest001.csproj內的ImplicitUsings改為disable
(enable時有些using指示詞會被隱藏,細節請參考微軟的介紹
)

Step7:影片17:50,指令dotnet restore,在.vscode資料夾內建立launch.json和tasks.json

Step8:影片17:52,先設定中斷點(breakpoint),再輸入指令dotnet run,執行並偵錯程式

Step9:影片17:56,中斷點攔截成功,第一個CSharp程式,HelloWorld正常執行

Program.cs:
using System;
namespace UnitTest001;
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello, World!");
    }
}

UnitTest001.csproj:
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>disable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
</Project>

launch.json:
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            // If you have changed target frameworks, make sure to update the program path.
            "program": "${workspaceFolder}/bin/Debug/net7.0/UnitTest001.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
            "console": "internalConsole",
            "stopAtEntry": false
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach"
        }
    ]
}

tasks.json:
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "command": "dotnet",
            "type": "process",
            "args": [
                "build",
                "${workspaceFolder}/UnitTest001.csproj",
                "/property:GenerateFullPaths=true",
                "/consoleloggerparameters:NoSummary"
            ],
            "problemMatcher": "$msCompile"
        },
        {
            "label": "publish",
            "command": "dotnet",
            "type": "process",
            "args": [
                "publish",
                "${workspaceFolder}/UnitTest001.csproj",
                "/property:GenerateFullPaths=true",
                "/consoleloggerparameters:NoSummary"
            ],
            "problemMatcher": "$msCompile"
        },
        {
            "label": "watch",
            "command": "dotnet",
            "type": "process",
            "args": [
                "watch",
                "run",
                "--project",
                "${workspaceFolder}/UnitTest001.csproj"
            ],
            "problemMatcher": "$msCompile"
        }
    ]
}
送禮物贊助創作者 !
0
留言

創作回應

更多創作