.NET Core CLI 的 F # 入门Get started with F# with the .NET Core CLI
本文介绍如何在 .NET Core CLI (Windows、macOS 或 Linux) 的任何操作系统上开始处理 F #。This article covers how you can get started with F# on any operating system (Windows, macOS, or Linux) with the .NET Core CLI. 其中介绍了如何使用控制台应用程序调用的类库构建多项目解决方案。It goes through building a multi-project solution with a class library that is called by a console application.
先决条件Prerequisites
若要开始,必须安装最新 .NET Core SDK。To begin, you must install the latest .NET Core SDK.
本文假设你知道如何使用命令行并具有首选文本编辑器。This article assumes that you know how to use a command line and have a preferred text editor. 如果尚未使用该选项, Visual Studio Code 是 F # 的文本编辑器。If you don't already use it, Visual Studio Code is a great option as a text editor for F#.
构建简单的多项目解决方案Build a simple multi-project solution
打开命令提示符/终端,并使用 dotnet new 命令创建名为的新解决方案文件 FSNetCore
:Open a command prompt/terminal and use the dotnet new command to create new solution file called FSNetCore
:
dotnet new sln -o FSNetCore
运行上述命令后,将生成以下目录结构:The following directory structure is produced after running the previous command:
FSNetCore
├── FSNetCore.sln
编写类库Write a class library
将目录更改为 FSNetCore。Change directories to FSNetCore.
使用 dotnet new
命令在名为 library 的 src 文件夹中创建一个类库项目。Use the dotnet new
command, create a class library project in the src folder named Library.
dotnet new classlib -lang "F#" -o src/Library
运行上述命令后,将生成以下目录结构:The following directory structure is produced after running the previous command:
└── FSNetCore
├── FSNetCore.sln
└── src
└── Library
├── Library.fs
└── Library.fsproj
将的内容替换为 Library.fs
以下代码:Replace the contents of Library.fs
with the following code:
module Library
open Newtonsoft.Json
let getJsonNetJson value =
let json = JsonConvert.SerializeObject(value)
$"I used to be {value} but now I'm {json} thanks to JSON.NET!"
将 NuGet 包上的 Newtonsoft.Js添加到库项目。Add the Newtonsoft.Json NuGet package to the Library project.
dotnet add src/Library/Library.fsproj package Newtonsoft.Json
Library
FSNetCore
使用dotnet .sln add命令将项目添加到解决方案:Add the Library
project to the FSNetCore
solution using the dotnet sln add command:
dotnet sln add src/Library/Library.fsproj
运行 dotnet build
以生成项目。Run dotnet build
to build the project. 生成时将还原未解析的依赖项。Unresolved dependencies will be restored when building.
编写使用类库的控制台应用程序Write a console application that consumes the class library
使用 dotnet new
命令在名为 "应用" 的 src 文件夹中创建一个控制台应用程序。Use the dotnet new
command, create a console application in the src folder named App.
dotnet new console -lang "F#" -o src/App
运行上述命令后,将生成以下目录结构:The following directory structure is produced after running the previous command:
└── FSNetCore
├── FSNetCore.sln
└── src
├── App
│ ├── App.fsproj
│ ├── Program.fs
└── Library
├── Library.fs
└── Library.fsproj
将 Program.fs
文件的内容替换为以下代码:Replace the contents of the Program.fs
file with the following code:
open System
open Library
[<EntryPoint>]
let main argv =
printfn "Nice command-line arguments! Here's what JSON.NET has to say about them:"
for arg in argv do
let value = getJsonNetJson arg
printfn $"{value}"
0 // return an integer exit code
Library
使用dotnet 添加引用添加对项目的引用。Add a reference to the Library
project using dotnet add reference.
dotnet add src/App/App.fsproj reference src/Library/Library.fsproj
App
使用命令将项目添加到 FSNetCore
解决方案 dotnet sln add
:Add the App
project to the FSNetCore
solution using the dotnet sln add
command:
dotnet sln add src/App/App.fsproj
还原 NuGet 依赖项, dotnet restore
然后运行 dotnet build
以生成项目。Restore the NuGet dependencies, dotnet restore
and run dotnet build
to build the project.
将目录更改为 src/App
控制台项目,并运行作为参数传递的项目 Hello World
:Change directory to the src/App
console project and run the project passing Hello World
as arguments:
cd src/App
dotnet run Hello World
应该看到以下结果:You should see the following results:
Nice command-line arguments! Here's what JSON.NET has to say about them:
I used to be Hello but now I'm ""Hello"" thanks to JSON.NET!
I used to be World but now I'm ""World"" thanks to JSON.NET!
后续步骤Next steps
接下来,请查看 F # 教程 ,了解有关不同 F # 功能的详细信息。Next, check out the Tour of F# to learn more about different F# features.