You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
PolyglotNotebooksStudy/Docs/多语言笔记.1.1.入门说明.ipynb

639 lines
14 KiB
Plaintext

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Jupyter笔记 简单入门\n",
"=============================="
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"使用 Jupyter NoteBook 形式,优点是代码执行和 Markdown 文档放在同一个文件中,并可以交替显示和分段执行代码。"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## 显式声明使用 C# 语言\n",
"+ csharp\n",
"+ fsharp\n",
"+ pwsh"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"#!csharp"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## C# 中的引用\n",
"+ 导入程序文件\n",
" > 可以导入多种文件,包括 .cs .csc .fc .js等语法为` #!import /path/to/file ` \n",
"+ 引用本机 Dll 文件\n",
" > 可以导入本机编译好的dll文件语法为 `#!import /path/to/file`\n",
"+ 引用 Nuget 包\n",
"+ > 默认包源下可以导入nuget,当然也可以设置包源,语法为 `#r 包名,可选版本号`"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## C# 中的引用\n",
"+ 导入程序文件\n",
" > 可以导入多种文件,包括 .cs .csc .fc .js等语法为` #!import /path/to/file ` "
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"#! import \"../NotebookStudy.ConsoleApp/Persion.cs\""
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"ename": "Error",
"evalue": "(2,13): error CS0246: 未能找到类型或命名空间名“Person”(是否缺少 using 指令或程序集引用?)",
"output_type": "error",
"traceback": [
"(2,13): error CS0246: 未能找到类型或命名空间名“Person”(是否缺少 using 指令或程序集引用?)"
]
}
],
"source": [
"//单纯引用.cs文件不能直接使用解决方法待查找\n",
"var p = new Person()\n",
"{\n",
" Id=1,\n",
" Name=\"小张\",\n",
" Address=\"上海无名路1号\",\n",
" Age = 28\n",
"};\n",
"//在NoteBook中可以格式化显示(Notebook提供) C#对象:使用 display(对象); 如果在代码结尾的话,可以直接写 C#对象。\n",
"display(p);\n",
"\n",
"//代码结尾的话,直接写 对象\n",
"p"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"data": {
"text/html": [
"<details open=\"open\" class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>{ Name = xx, Agx = 33 }</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>Name</td><td>xx</td></tr><tr><td>Agx</td><td><div class=\"dni-plaintext\"><pre>33</pre></div></td></tr></tbody></table></div></details><style>\r\n",
".dni-code-hint {\r\n",
" font-style: italic;\r\n",
" overflow: hidden;\r\n",
" white-space: nowrap;\r\n",
"}\r\n",
".dni-treeview {\r\n",
" white-space: nowrap;\r\n",
"}\r\n",
".dni-treeview td {\r\n",
" vertical-align: top;\r\n",
" text-align: start;\r\n",
"}\r\n",
"details.dni-treeview {\r\n",
" padding-left: 1em;\r\n",
"}\r\n",
"table td {\r\n",
" text-align: start;\r\n",
"}\r\n",
"table tr { \r\n",
" vertical-align: top; \r\n",
" margin: 0em 0px;\r\n",
"}\r\n",
"table tr td pre \r\n",
"{ \r\n",
" vertical-align: top !important; \r\n",
" margin: 0em 0px !important;\r\n",
"} \r\n",
"table th {\r\n",
" text-align: start;\r\n",
"}\r\n",
"</style>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"var x = new {Name=\"xx\",Agx=33};\n",
"display(x);"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"+ 引用本机 Dll 文件\n",
" > 可以导入本机编译好的dll文件语法为 `#!import /path/to/file`"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"data": {
"text/html": [
"<details open=\"open\" class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>NotebookStudy.ConsoleApp.Person</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>Id</td><td><div class=\"dni-plaintext\"><pre>2</pre></div></td></tr><tr><td>Name</td><td>本山</td></tr><tr><td>Address</td><td><div class=\"dni-plaintext\"><pre>&lt;null&gt;</pre></div></td></tr><tr><td>Age</td><td><div class=\"dni-plaintext\"><pre>55</pre></div></td></tr></tbody></table></div></details><style>\r\n",
".dni-code-hint {\r\n",
" font-style: italic;\r\n",
" overflow: hidden;\r\n",
" white-space: nowrap;\r\n",
"}\r\n",
".dni-treeview {\r\n",
" white-space: nowrap;\r\n",
"}\r\n",
".dni-treeview td {\r\n",
" vertical-align: top;\r\n",
" text-align: start;\r\n",
"}\r\n",
"details.dni-treeview {\r\n",
" padding-left: 1em;\r\n",
"}\r\n",
"table td {\r\n",
" text-align: start;\r\n",
"}\r\n",
"table tr { \r\n",
" vertical-align: top; \r\n",
" margin: 0em 0px;\r\n",
"}\r\n",
"table tr td pre \r\n",
"{ \r\n",
" vertical-align: top !important; \r\n",
" margin: 0em 0px !important;\r\n",
"} \r\n",
"table th {\r\n",
" text-align: start;\r\n",
"}\r\n",
"</style>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"//如果找不到文件,需要先生成 NotebookStudy.ConsoleApp 项目\n",
"\n",
"#r \"../NotebookStudy.ConsoleApp/bin/Debug/net7.0/NotebookStudy.ConsoleApp.dll\"\n",
"\n",
"using NotebookStudy.ConsoleApp;\n",
"\n",
"var personA= new Person()\n",
"{\n",
" Id=2,\n",
" Name=\"本山\",\n",
" Age=55\n",
"};\n",
"\n",
"personA"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"+ 引用 Nuget 包"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"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"
},
{
"data": {
"text/plain": [
"{\"Id\":2,\"Name\":\"newtonsoft类库\",\"Age\":6}"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"//安装库的最新版本\n",
"#r \"nuget:newtonsoft.json\"\n",
"var jsonObj = new {Id=2,Name=\"newtonsoft类库\", Age=6};\n",
"\n",
"//使用库\n",
"var jsonText = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj);\n",
"jsonText"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"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><li><span>System.Text.Json, 4.7.2</span></li></ul></div></div>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"{\"Id\":2,\"Name\":\"System.Text.Json\\u7C7B\\u5E93\",\"Age\":6}"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"//安装库的指定版本\n",
"#r \"nuget:System.Text.Json,4.7.2\"\n",
"\n",
"//使用库\n",
"var jsonObj2 = new {Id=2,Name=\"System.Text.Json类库\", Age=6};\n",
"var jsonText2 = System.Text.Json.JsonSerializer.Serialize(jsonObj2);\n",
"jsonText2"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"data": {
"text/html": [
"<div><div></div><div></div><div><strong>Installed Packages</strong><ul><li><span>xunit, 2.4.2</span></li></ul></div></div>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"//安装最新的预览版库\n",
"#r \"nuget:xunit,*-*\"\n",
"\n",
"using Xunit;\n",
"using Xunit.Abstractions;\n",
"using Xunit.Sdk;\n",
"\n",
"public class UnitTest1\n",
"{\n",
" [Fact]\n",
" public void Test1()\n",
" {\n",
" Assert.True(true, \"xxxx\");\n",
" }\n",
"}"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Nuget 包源管理\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"+ 默认 Nuget 包源\n",
" > 默认包源可能是https://api.nuget.org/v3/index.json 使用默认包源的话,可以不添加包源引用。当然也可以添加。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"#r \"nuget:AutoFixture.Xunit2\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"//也可以指定默认包源\n",
"#i \"nuget:https://api.nuget.org/v3/index.json\"\n",
"\n",
"#r \"nuget:AutoFixture.Xunit2\""
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"+ 本机包源 Nuget"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"//添加本地包源\n",
"#i \"nuget:C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\\"\n",
"\n",
"//使用包源\n",
"#r \"nuget:xunit\"\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"+ 经过身份验证的Nuget包源\n",
" > 不能直接引用验证包源,可以通过将源的 PAT 放入用户级 nuget.config 文件来访问经过身份验证的源"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## 内建魔术命令"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"+ #!about 显示有关内核版本的信息"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"#!about"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"+ #!lsmagic 列出可用的魔术命令,包括可能已通过扩展安装的命令"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"#!lsmagic"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"+ #!time 显示执行时间"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"#!time"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"+ -h 参数 显示命令的帮助信息"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"#!time -h"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"language_info": {
"name": "polyglot-notebook"
},
"polyglot_notebook": {
"kernelInfo": {
"defaultKernelName": "csharp",
"items": [
{
"aliases": [],
"name": ".NET"
},
{
"aliases": [
"C#",
"c#"
],
"languageName": "C#",
"name": "csharp"
},
{
"aliases": [
"js"
],
"languageName": "JavaScript",
"name": "javascript"
},
{
"aliases": [],
"languageName": "KQL",
"name": "kql"
},
{
"aliases": [
"frontend"
],
"name": "vscode"
},
{
"aliases": [],
"name": "webview"
}
]
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}