master
bicijinlian 1 week ago
parent 95f9fd0c5b
commit 091a41543d

Binary file not shown.

After

Width:  |  Height:  |  Size: 216 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 102 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 254 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 103 KiB

@ -0,0 +1,137 @@
Jupyter笔记 简单入门
==============================
使用 Jupyter NoteBook 形式,优点是代码执行和 Markdown 文档放在同一个文件中,并可以交替显示和分段执行代码。
## 显式声明使用 C# 语言
+ csharp
+ fsharp
+ pwsh
```csharp
#!csharp
```
## C# 中的引用
+ 导入程序文件
> 可以导入多种文件,包括 .cs .csc .fc .js等语法为` #!import /path/to/file `
+ 引用本机 Dll 文件
> 可以导入本机编译好的dll文件语法为 `#!import /path/to/file`
+ 引用 Nuget 包
+ > 默认包源下可以导入nuget,当然也可以设置包源,语法为 `#r 包名,可选版本号`
## C# 中的引用
+ 导入程序文件
> 可以导入多种文件,包括 .cs .csc .fc .js等语法为` #!import /path/to/file `
```csharp
#! import "../NotebookStudy.ConsoleApp/Persion.cs"
```
```csharp
//单纯引用.cs文件不能直接使用解决方法待查找
var p = new Person()
{
Id=1,
Name="小张",
Address="上海无名路1号",
Age = 28
};
//在NoteBook中可以格式化显示(Notebook提供) C#对象:使用 display(对象); 如果在代码结尾的话,可以直接写 C#对象。
display(p);
//代码结尾的话,直接写 对象
p
```
```csharp
var x = new {Name="xx",Agx=33};
display(x);
```
+ 引用本机 Dll 文件
> 可以导入本机编译好的dll文件语法为 `#!import /path/to/file`
```csharp
//如果找不到文件,需要先生成 NotebookStudy.ConsoleApp 项目
#r "../NotebookStudy.ConsoleApp/bin/Debug/net7.0/NotebookStudy.ConsoleApp.dll"
using NotebookStudy.ConsoleApp;
var personA= new Person()
{
Id=2,
Name="本山",
Age=55
};
personA
```
+ 引用 Nuget 包
```csharp
//安装库的最新版本
#r "nuget:newtonsoft.json"
var jsonObj = new {Id=2,Name="newtonsoft类库", Age=6};
//使用库
var jsonText = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj);
jsonText
```
```csharp
//安装库的指定版本
#r "nuget:System.Text.Json,4.7.2"
//使用库
var jsonObj2 = new {Id=2,Name="System.Text.Json类库", Age=6};
var jsonText2 = System.Text.Json.JsonSerializer.Serialize(jsonObj2);
jsonText2
```
```csharp
//安装最新的预览版库
#r "nuget:xunit,*-*"
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;
public class UnitTest1
{
[Fact]
public void Test1()
{
Assert.True(true, "xxxx");
}
}
```
## Nuget 包源管理
+ 在线包源
> 默认包源https://api.nuget.org/v3/index.json 使用默认包源的话,可以不添加包源引用。当然也可以添加。
```csharp
//添加包源:可以添加多个
#i "nuget:https://api.nuget.org/v3/index.json"
```
```csharp
//使用包源
#r "nuget:AutoFixture.Xunit2"
```
+ 本机包源 Nuget
```csharp
//添加本地包源
#i "nuget:C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\"
//使用包源
#r "nuget:xunit"
```
+ 经过身份验证的Nuget包源
> 不能直接引用验证包源,可以通过将源的 PAT 放入用户级 nuget.config 文件来访问经过身份验证的源
## 内建魔术命令
+ #!about 显示有关内核版本的信息
```csharp
#!about
```
+ #!lsmagic 列出可用的魔术命令,包括可能已通过扩展安装的命令
```csharp
#!lsmagic
```
+ #!time 显示执行时间
```csharp
#!time
```
+ -h 参数 显示命令的帮助信息
```csharp
#!time -h
```

@ -1,38 +0,0 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "065bfcac",
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"source": [
"使用NuGet包\n",
"==========="
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"polyglot_notebook": {
"kernelInfo": {
"defaultKernelName": "csharp",
"items": [
{
"aliases": [],
"languageName": "csharp",
"name": "csharp"
}
]
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}

@ -0,0 +1,324 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "065bfcac",
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"source": [
"管理NuGet包\n",
"==========="
]
},
{
"cell_type": "markdown",
"id": "0baca368",
"metadata": {},
"source": [
"## 浏览和查找NuGet包"
]
},
{
"cell_type": "markdown",
"id": "18559f3e",
"metadata": {},
"source": [
"VS Code 提供了查看和查找NuGet包的UI界面打开一个`Polyglot Notebook`文件,打开一个\"终端\", 切换到 \"nuget\"项打开一个UI管理窗口。如下图\n",
"\n",
"![UI窗口](./assets/images/NugetUI.jpg)"
]
},
{
"cell_type": "markdown",
"id": "c110c37f",
"metadata": {},
"source": [
"## 导入 NuGet 包"
]
},
{
"cell_type": "markdown",
"id": "7eecdf67",
"metadata": {},
"source": [
"在`.NET Interactive`中C#和F#核允许你使用`#r nuget`魔法命令将NuGet包导入到你的交互式会话中。两种语言的语法相同。"
]
},
{
"cell_type": "markdown",
"id": "73546727",
"metadata": {},
"source": [
"+ 导入最新版本的包,使用` #r nuget` 而无需指定版本号:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "ede3455f",
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"data": {
"text/html": [
"<div><div></div><div></div><div><strong>Installed Packages</strong><ul><li><span>Newtonsoft.Json, 13.0.3</span></li></ul></div></div>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#r \"nuget:Newtonsoft.Json\""
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{\"Name\":\"张三\",\"Age\":20},{\"Name\":\"李四\",\"Age\":69},{\"Name\":\"王五\",\"Age\":82}]\r\n"
]
}
],
"source": [
"//使用包:序列化一个匿名类集合\n",
"\n",
"//引入命名空间\n",
"using Newtonsoft.Json;\n",
"\n",
"{ //花括号分隔作用域\n",
" var persons = new[]\n",
" {\n",
" new {Name=\"张三\", Age = 20},\n",
" new {Name=\"李四\", Age = 69},\n",
" new {Name=\"王五\", Age = 82},\n",
" }; \n",
"\n",
" var jsonText = Newtonsoft.Json.JsonConvert.SerializeObject(persons);\n",
" Console.WriteLine(jsonText);\n",
"}\n"
]
},
{
"cell_type": "markdown",
"id": "da4b55e2",
"metadata": {},
"source": [
"+ 导入指定版本的包"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"data": {
"text/html": [
"<div><div></div><div></div><div><strong>Installed Packages</strong><ul><li><span>Newtonsoft.Json, 13.0.3</span></li></ul></div></div>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#r \"nuget:Newtonsoft.Json,13.0.3\""
]
},
{
"cell_type": "markdown",
"id": "07da3486",
"metadata": {},
"source": [
"+ 导入预览版本的包"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"data": {
"text/html": [
"<div><div><strong>Restore sources</strong><ul><li><span>https://api.nuget.org/v3/index.json</span></li></ul></div><div></div><div><strong>Installed Packages</strong><ul><li><span>System.CommandLine, 2.0.0-beta4.22272.1</span></li></ul></div></div>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#r \"nuget:System.CommandLine,*-*\""
]
},
{
"cell_type": "markdown",
"id": "fe5a81be",
"metadata": {},
"source": [
"## 设置包源"
]
},
{
"cell_type": "markdown",
"id": "c8ce789d",
"metadata": {},
"source": [
"如果包不是Nuget官方托管包或者想明确包的来源使用`#i`命令指定一个nuget包源"
]
},
{
"cell_type": "markdown",
"id": "b2e11743",
"metadata": {},
"source": [
"+ 默认包源\n",
" 可以不用设置,默认使用的官方托管包"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"data": {
"text/html": [
"<div><div><strong>Restore sources</strong><ul><li><span>https://api.nuget.org/v3/index.json</span></li></ul></div><div></div><div></div></div>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"//默认包源:可以不设置\n",
"#i \"nuget:https://api.nuget.org/v3/index.json\""
]
},
{
"cell_type": "markdown",
"id": "39096d0b",
"metadata": {},
"source": [
"+ 远程包源\n",
"\n",
" 组织通常会将包存储在私人或预发布源中"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"data": {
"text/html": [
"<div><div><strong>Restore sources</strong><ul><li><span>https://api.nuget.org/v3/index.json</span></li><li><span>https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json</span></li></ul></div><div></div><div></div></div>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"//添加dotnet项目的预发布nuget源\n",
"#i \"nuget:https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json\""
]
},
{
"cell_type": "markdown",
"id": "134c7d97",
"metadata": {},
"source": [
"+ 本地包源\n",
"\n",
" 可以将一个本地文件夹作为nuget源"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"//\"C:\\Users\\用户账号\\.nuget\\packages\" 是 Windows 系统中 visual studio 默认的本机 nuget 包存储位置,可以当本地包源(注意路径用户账号换成自己的)\n",
"//当然可使用任意文件夹里面要有nuget包\n",
"\n",
"#i \"nuget:C:\\Users\\ruyu\\.nuget\\packages\"\n",
"#r \"nuget:MyOrg.MyPackage\""
]
},
{
"cell_type": "markdown",
"id": "54d60e85",
"metadata": {},
"source": [
"+ 需要认证的包源\n",
"\n",
" 一些企业搭建有自己的nuget私有包服务器(MyGet等),使用时需要认证。\n",
"\n",
" 但是:.NET Interactive 不直接支持访问需要身份验证的包源,但你可以通过在用户级别的 nuget.config 文件中放入包源的 PAT 来访问经过身份验证的源。你可以在这里了解更多关于这种方法的信息"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"polyglot_notebook": {
"kernelInfo": {
"defaultKernelName": "csharp",
"items": [
{
"aliases": [],
"languageName": "csharp",
"name": "csharp"
}
]
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}

@ -64,7 +64,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
@ -85,7 +85,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {
"dotnet_interactive": {
"language": "pwsh"
@ -94,14 +94,22 @@
"kernelName": "pwsh"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"192.168.1.1\r\n"
]
}
],
"source": [
"# Poweshell中使用 前面C#单元中定义的变量\n",
"# 特别注意因为PS中变量名必须以$开头,所以在命令中 name 参数名在PS中使用时必须加$前辍\n",
"\n",
"#!set --value @csharp:getway --name gw\n",
"\n",
"Write-Host $gw\n"
"Write-Host $gw"
]
},
{
@ -113,7 +121,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
@ -122,11 +130,19 @@
"kernelName": "fsharp"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"192.168.1.1\r\n"
]
}
],
"source": [
"#!set --value @csharp:getway --name getway\n",
"\n",
"Console.WriteLine(getway)\n"
"Console.WriteLine(getway)"
]
},
{
@ -147,10 +163,20 @@
"kernelName": "javascript"
}
},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"192.168.1.1"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#!set --value @csharp:getway --name getway\n",
"console.log(getway);\n"
"console.log(getway);"
]
}
],

Loading…
Cancel
Save