目录
一、Solution Explorer插件应用示例。
准备工作:需要为VS Code安装C#插件和Solution-Explorer插件。
在“扩展”选项卡中输入关键字“C#”,安装右侧的插件以便支持.NET开发。
Identifier:ms-dotnettools.csharp
在“扩展”选项卡中输入关键字“vscode-solution-explorer”,安装右侧的插件以便在VS Code左侧工具栏中方便创建新项目和解决方案。
Identifier:fernandoescolar.vscode-solution-explorer
1.1 创建空白solution示例。
创建一个空白解决方案 SimpleCalculator 。
点击“Open solution”打开一个已存在的项目,点击“Create New Solution”创建一个新的解决方案。或者使用快捷键Command + Shift + P,输入“create new empty solution”,输入新的解决方案名称“SimpleCalculator”。
注意:在创建空白解决方案前,VS Code的终端要切换到 NewSolutionTestDemo 下,这时添加solution才会出现在 NewSolutionTestDemo 文件夹下。
创建新的解决方案“SimpleCalculator”成功后,之前安装的Solution Explorer扩展将在VS Code终端自动执行以下命令:
dotnet new sln -n "SimpleCalculator"
1.2 在solution中添加类库示例。
向空白解决方案 SimpleCalculator 中添加类库 MathOperations 。
右键单击解决方案(在Solution Explorer窗格中),然后从上下文菜单中选择Add new project选项。
这将列出.NET CLI提供的可用项目类型(参见下图),选择“类库”选项。
系统将询问使用哪种语言,选择C#,编辑器将提示输入项目名称,将它设置为MathOperations后回车,此时类库已添加到解决方案中。
Solution Explorer扩展将在VS Code终端自动执行以下命令:
dotnet new "classlib" -lang "C#" -n "MathOperations" -o "MathOperations"
cd "/Users/xxx/Projects/NewSolutionTestDemo"
dotnet sln "/Users/xxx/Projects/NewSolutionTestDemo/SimpleCalculator.sln" add "/Users/xxx/Projects/NewSolutionTestDemo/MathOperations/MathOperations.csproj"
1.3 在solution中添加控制台应用示例。
重复以上步骤,向空白解决方案 SimpleCalculator 中添加控制台应用程序 Calculator 。
Solution Explorer扩展将在VS Code终端自动执行以下命令:
cd "/Users/xxx/Projects/NewSolutionTestDemo"
dotnet new "console" -lang "C#" -n "Calculator" -o "Calculator"
cd "/Users/xxx/Projects/NewSolutionTestDemo"
dotnet sln "/Users/xxx/Projects/NewSolutionTestDemo/SimpleCalculator.sln" add "/Users/xxx/Projects/NewSolutionTestDemo/Calculator/Calculator.csproj"
到Solution Explorer中查看我们新建的MathOperations类库和Calculator控制台应用程序。