在Mac OS上开发 .NET Core
参考 Homebrew 简介 安装 Homebrew。(参考资料: Getting started with .NET Core on macOS )
在 Mac 上安装 .NET Core SDK (参考资料: .NET Core installation guide On Mac OS )
$ brew update
$ brew install openssl
$ mkdir -p /usr/local/lib
$ ln -s /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib /usr/local/lib/
$ ln -s /usr/local/opt/openssl/lib/libssl.1.0.0.dylib /usr/local/lib/
下载 .NET Core SDK 。
从 Hello World 程序开始:
$ dotnet new console -o hwapp
$ cd hwapp
$ dotnet restore
$ dotnet run
安装 Visual Studio Code C# Extension,在VS Code 按 command + p
,然后输入 ext install c#
并回车确认再重新启动。重启后会自动更新 C# 的依赖项:Mono Runtime 、Mono Framework、OmniSharp、NET Core Debugger。
.NET Core Tutorial
在VS Code开发环境中按 「 control + `」 或者点击 「查看->集成终端」 菜单以 显示控制台:
#创建目录
$ mkdir tutorial
#注意:一定要切换到子目录,否则会在当前目录下创建当前目录名的解决方案
$ cd tutorial
#创建一个解决方案,方案名是 tutorial.sln,即当前目录的名称
$ dotnet new sln
创建类库及测试
#在library目录下创建一个library的类库项目,library既是项目名也是目录名
$ dotnet new classlib -o library
#将library项目添加到解决方案
$ dotnet sln add library/library.csproj
#为library项目添加引用程序包 Newtonsoft.Json
$ dotnet add library package Newtonsoft.Json
#恢复项目的依赖项
$ dotnet restore
#重命名Class1.cs -> Thing.cs
$ mv Class1.cs Thing.cs
#在Thing.cs中输入如下代码
using static Newtonsoft.Json.JsonConvert;
namespace Library
{
public class Thing
{
public int Get(int left, int right) =>
DeserializeObject<int>($"{left + right}");
}
}
#编译解决方案
$ dotnet build
#创建测试项目
$ dotnet new xunit -o library-test
#将测试项目添加到解决方案
$ dotnet add library-test/library-test.csproj
#为测试项目添加项目引用
$ dotnet add library-test/library-test.csproj reference library/library.csproj
#为UnitTest1.cs为如下测试方法
using Library;
[Fact]
public void TestThing() {
Assert.NotEqual(42, new Thing().Get(19, 23));
}
#再次执行恢复项目的依赖项命令
$ dotnet restore
#执行测试,查看测试结果
$ dotnet test library-test/library-test.csproj
#测试结果:Total tests: 1. Passed: 0. Failed: 1. Skipped: 0.
#修改测试方法中的 NotEqual 为 Equal 再次测试查看结果如何...
创建控制台程序
#在app目录下创建一个app的控制台项目,app既是项目名也是目录名
$ dotnet new console -o app
#将项目添加到解决方案
$ dotnet sln add app/app.csproj
#为项目添加项目引用
$ dotnet add app/app.csproj reference library/library.csproj
#执行恢复项目的依赖项命令
$ dotnet restore
#为Program.cs的引用和Main方法添加如下语句:
using static System.Console;
Console.WriteLine($"The answer is {new Thing().Get(19, 23)}");
#执行程序。「 -p 」参数是指运行指定的项目
$ dotnet run -p app/app.csproj
多项目调试
在VS Code中每次只能调试一个解决方案(或文件夹),配置好 launch.json 和 tasks.json 需要重新启动?(未重新启动有错误)
# launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceRoot}/app/bin/Debug/netcoreapp1.1/app.dll",
"args": [],
"cwd": "${workspaceRoot}/app",
"stopAtEntry": false,
"console": "internalConsole"
}
]
}
#tasks.json
{
"version": "0.1.0",
"command": "dotnet",
"isShellCommand": true,
"args": [],
"tasks": [
{
"taskName": "build",
"args": [ ],
"isBuildCommand": true,
"showOutput": "silent",
"problemMatcher": "$msCompile"
}
]
}
利用文件夹组织代码
一般来说,如果项目的规模达到一定程度,可以考虑分别管理源代码和测试项目,例如:
/NewTypes
|__/src
|__/NewTypes #命名空间
|__/Pets #命名空间
|__Dog.cs
|__Cat.cs
|__IPet.cs
|__Program.cs
|__NewTypes.csproj
|__/test
|__NewTypesTests
|__PetTests.cs
|__NewTypesTests.csproj
#可以通过以下命令来建立测试项目
$ mkdir test
$ cd test
$ dotnet new xunit -o MyTypesTests #初始化测试项目
$ cd MyTypesTest
$ dotnet restore
$ dotnet test
卸载 NetCore
## dotnet-uninstall-pkgs.sh
#!/usr/bin/env bash
#
# Copyright (c) .NET Foundation and contributors. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for full license information.
#
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
current_userid=$(id -u)
if [ $current_userid -ne 0 ]; then
echo "$(basename "$0") uninstallation script requires superuser privileges to run" >&2
exit 1
fi
# this is the common suffix for all the dotnet pkgs
dotnet_pkg_name_suffix="com.microsoft.dotnet"
dotnet_install_root="/usr/local/share/dotnet"
dotnet_path_file="/etc/paths.d/dotnet"
remove_dotnet_pkgs(){
installed_pkgs=($(pkgutil --pkgs | grep $dotnet_pkg_name_suffix))
for i in "${installed_pkgs[@]}"
do
echo "Removing dotnet component - \"$i\"" >&2
pkgutil --force --forget "$i"
done
}
remove_dotnet_pkgs
[ "$?" -ne 0 ] && echo "Failed to remove dotnet packages." >&2 && exit 1
echo "Deleting install root - $dotnet_install_root" >&2
rm -rf "$dotnet_install_root"
rm -f "$dotnet_path_file"
echo "dotnet packages removal succeeded." >&2
exit 0