//教学或演示时,常有对比功能,代码基本相同:花括号可以使用相同的变量名而不冲突
{ //测试 where
var source = "你 是 我的 宝儿 !".Split(" ");
var result = source.Where(s=> s.Length<=1).Count();
result.Display();
}
{ //测试 FirstOrDefault
var source = "你 是 我的 宝儿 !".Split(" ");
var result = source.FirstOrDefault(s => s != "!");
result.Display();
}
//折叠整个代码块,方便全局查看
{
//多行业务代码
}
把共用的部分独立出来,方便统一管理和引用。
//初始化单元格:必须先执行一次
#!import "./Base.ipynb"
//导入文件
#!import "./shared/script/tools.cs"
//使用功能
{
var path = Tools.GetCurrentPath();
Console.WriteLine($"当前路径:{path}");
}
#r "nuget:Microsoft.DotNet.Interactive.SQLite,*-*"
/* connect 和 import 等命令,只能执行一次
#!connect sqlite --kernel-name SQLiteSharedKernel --connection-string "Data Source=.\assets\database\study.db;"
*/
using Microsoft.DotNet.Interactive;
using Microsoft.DotNet.Interactive.Commands;
//动态方法:可执行多次,且可使用变量等功能
{
//内核名:魔法命令中的内核名,执行后会自动加 sql- 前缀,做为内核名被使用
string magicCommandKernelName = "SQLiteSharedKernel";
string completeKernelName = "sql-" + magicCommandKernelName;
//引入内核:可重复执行
if(Microsoft.DotNet.Interactive.Kernel.Root.FindKernelByName(completeKernelName) == null)
{
var connectKernelCode = $"#!connect sqlite --kernel-name {magicCommandKernelName} --connection-string \"{SharedDbConnect.SQLiteConnectionString}\"";
await Kernel.Root.SendAsync(new SubmitCode( connectKernelCode, "csharp"));
}
else
{
Console.WriteLine($"名为 {completeKernelName} 的内核已存在。需要新内核时,请为--kernel-name参数使用不同的值, 本次执行不做任何更改!");
}
}
//可动态添加单元格
using Microsoft.DotNet.Interactive;
using Microsoft.DotNet.Interactive.Commands;
{//每执行一次,在下方添加代码单元格:可执行的
var command = new SendEditableCode(
"csharp",
"Console.WriteLine(\"我是动态生成的代码单元哈!\");");
var input = await Kernel.Root.SendAsync(command);
}
Console.WriteLine("我是动态生成的代码单元哈!");
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Reflection;
//查看各种程序路径
{
var pathDic = new Dictionary<string, (string desc, string path)>()
{
//当前运行的exe的完整路径,包含exe文件名,只用于WinForm
{"Application.ExecutablePath",("程序集基完整路径(仅WinForm)", "Application.ExecutablePath 只适用于WinForm") },
//程序的启动路径:只用于WinForm
{"Application.StartupPath",("程序集启动路径(仅WinForm)", "Application.StartupPath 只适用于WinForm") },
//当前执行的exe或启动项目的路径,通过AppContext
{"AppContext.BaseDirectory",("执行或启动路径", AppContext.BaseDirectory) },
//当前执行的exe的目录,不包含exe名,使用AppDomain
{"AppDomain.CurrentDomain.BaseDirectory",("程序集解析程序用于探测程序集的基目录", AppDomain.CurrentDomain.BaseDirectory) },
//程序安装或启动基目录 包含应用程序的目录的名称
{"AppDomain.CurrentDomain.SetupInformation.ApplicationBase",("程序安装或启动基目录", AppDomain.CurrentDomain.SetupInformation.ApplicationBase) },
//当前进程的主模块路径,包含exe名
{"Process.GetCurrentProcess().MainModule.FileName",("当前进程的主模块路径", Process.GetCurrentProcess()?.MainModule?.FileName) },
//环境变量:用户当前工作目录的完整限定路径
{"Environment.CurrentDirectory",("用户当前工作目录的完整限定路径", Environment.CurrentDirectory) },
//环境变量:当前exe的完整路径,包含exe名,通过命令行参数
{"Environment.GetCommandLineArgs()[0]",("当前exe的完整路径", Environment.GetCommandLineArgs()[0]) },
//当前工作目录的路径(可变)
{"Directory.GetCurrentDirectory",("当前工作目录的路径(可变)", Directory.GetCurrentDirectory()) },
//当前Assembly的加载路径,包含dll或exe名
{"Assembly.GetExecutingAssembly().Location",("当前Assembly的加载路径", Assembly.GetExecutingAssembly().Location) },
//入口程序集的路径
{"Assembly.GetEntryAssembly().Location",("入口程序集的路径", Assembly.GetEntryAssembly()?.Location) },
//已过时:当前程序集的CodeBase路径,可能为file URI格式
{"Assembly.GetExecutingAssembly().CodeBase",("当前程序集的CodeBase路径", Assembly.GetExecutingAssembly()?.CodeBase) },
//已过时:入口程序集的CodeBase路径,可能为file URI格式
{"Assembly.GetEntryAssembly().CodeBase",("入口程序集的CodeBase路径", Assembly.GetEntryAssembly()?.CodeBase) },
};
var message = string.Empty;
foreach (var item in pathDic)
{
message += $"{item.Key} => {item.Value.path}{Environment.NewLine}";
}
Console.WriteLine(message);
}
Polyglot Notebooks优点非常多,缺点也不少。有其非常适用的场景,也有其难以使用的地方。主要是用对地方,它就是神器!
咱们CSDN视频课程见!