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.
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": [
"# 变量共享"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"使用 .NET 交互式内核,可以在单个笔记本中以多种语言编写代码。为了利用每种语言的不同优势,您会发现在它们之间共享数据很有用。即一种语言的变量,可以在其它语言中使用。\n",
"默认情况下,.NET Interactive 支持多种不同的语言, 其中大多数语言都允许使用magic 命令`#!set` 和 `#!shared` 进行共享。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 各语言对变量共享的支持"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"| 语言 | 变量共享 |\n",
"| ---- | ---- |\n",
"| C# | 支持 |\n",
"| F# | 支持 |\n",
"| PowerShell | 支持 |\n",
"| JavaScript | 支持 |\n",
"| SQL | 支持 |\n",
"| KQL | 支持 |\n",
"| Python | 支持 |\n",
"| R | 支持 |\n",
"| HTML | 不支持 |\n",
"| Mermaid | 不支持 |"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 查看、管理全部变量方法"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![变量共享](./assets/images/shared.001.jpg)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 简单示例: C# 运行中的变量,被其它语言共享。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"//定义变量:存储网关\n",
"string getway = \"192.168.1.1\";"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## PowerShell 中使用"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "pwsh"
},
"polyglot_notebook": {
"kernelName": "pwsh"
}
},
"outputs": [],
"source": [
"# Poweshell中使用 前面C#单元中定义的变量\n",
"# 特别注意: 因为PS中变量名必须以$开头,所以在命令中 name 参数名, 在PS中使用时必须加$前辍\n",
"\n",
"#!set --value @csharp:getway --name gw\n",
"\n",
"Write-Host $gw\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## F# 中使用"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"polyglot_notebook": {
"kernelName": "fsharp"
}
},
"outputs": [],
"source": [
"#!set --value @csharp:getway --name getway\n",
"\n",
"Console.WriteLine(getway)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 在javascrip中使用"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "javascript"
},
"polyglot_notebook": {
"kernelName": "javascript"
}
},
"outputs": [],
"source": [
"#!set --value @csharp:getway --name getway\n",
"console.log(getway);\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"language_info": {
"name": "polyglot-notebook"
},
"orig_nbformat": 4,
"polyglot_notebook": {
"kernelInfo": {
"defaultKernelName": "csharp",
"items": [
{
"aliases": [],
"name": "csharp"
},
{
"aliases": [],
"name": "razor"
}
]
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}