dotnet script

应用场景

sh/bash实在写不习惯 我还是想写c#….

dotnet:

https://dotnet.microsoft.com/en-us/download

dotnet tool install -g dotnet-script

脚手架

dotnet script init

Note: 脚手架只支持在当前目录创建。

脚手架的目的是可以方便vscode进行调试。所以在目标机器上也可以直接写脚本而不用初始化脚手架

#r修改之后注意重启omnisharp 或重启vscode

引用包或dll

#r "nuget:package name"
#r "path to dll"

引用其他脚本

#load "nuget: script package"
#load "path to the csx"

获取脚本执行路径

public static string GetScriptPath([CallerFilePath] string path = null) => path;
public static string GetScriptFolder([CallerFilePath] string path = null) => Path.GetDirectoryName(path);

TIP: 放在单独的脚本文件 当你需要获取路径的时候直接#load这个脚本然后调用即可。

例子:

main.csx

#!/usr/bin/env dotnet-script
#r "nuget:NewtonSoft.JSON"
#load "./test.csx"
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

Console.WriteLine("Hello world!");

var t = JObject.Parse("{\"test\":1}");
Console.WriteLine(t["test"]);

Console.WriteLine(T());
Console.WriteLine(new TestClass1().T());

test.csx

#!/usr/bin/env dotnet-script
public class TestClass1 {
    public int T(){
        return 102;
     }
}

public int T() {
    return 100;
}
posted @   仙贝  阅读(146)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示