dotnet-exec 讓 C# 程序更簡單
Intro
dotnet-exec
是一個可以執行 C# 程序而不需要項目文件的命令行工具,并且你可以指定自定義的入口方法不僅僅是Main
方法
在 python/NodeJs 里,可以使用python xx.py
/node xx.js
來運行代碼,在 C# 中一般是需要項目文件才能dotnet run
運行代碼,這對于一些比較簡單的代碼來說會顯得麻煩很多,而dotnet-exec
則可以用來簡化這一場景,使得我們可以沒有項目文件也可以運行,我們就可以直接dotnet-execxx.cs
除此之外我們也可以自定義代碼的入口方法不限于Main方法,而且我們可以直接執行源代碼和遠程文件代碼
How it works
工作流程分為三步:
獲取代碼:目前支持本地代碼、遠程代碼以及原始代碼
代碼編譯:Roslyn 完成代碼動態編譯
代碼執行:基于 AssemblyLoadContext 的代碼執行
核心實現是基于 Roslyn 來完成動態編譯
編譯的時候分成三種情況
一種是代碼是有Main方法的Console應用,直接執行 Main?方法即可
一種是沒有 Main 方法的 DLL,需要自定義入口方法,執行自定義的入口方法
最后是Script代碼是由Roslyn的Scripting功能進行支持進行編譯和執行
Install/Update
最新的穩定版本:
dotnet?tool?update?-g?dotnet-execute
最新的預覽版本:
dotnet?tool?update?-g?dotnet-execute?--prerelease
Docker 支持
使用 docker 執行
docker?run?--rm?weihanli/dotnet-exec:latest?dotnet-exec?"1+1"
docker?run?--rm?weihanli/dotnet-exec:latest?dotnet-exec?"Guid.NewGuid()"
docker?run?--rm?--pull=always?weihanli/dotnet-exec:latest?dotnet-exec?"ApplicationHelper.RuntimeInfo"
完整的 tag 列表請參考 https://hub.docker.com/r/weihanli/dotnet-exec/tags
除了 latest?tag 你也可以使用?0.12.0 這樣的版本 tag,docker 版本 tag 只發布穩定版本
Examples
Get started
執行本地文件:
dotnet-exec?HttpPathJsonSample.cs
執行本地文件并且自定義入口方法:
dotnet-exec?'HttpPathJsonSample.cs'?--entry?MainTest
詳細示例:
執行遠程文件:
dotnet-exec?'https://github.com/WeihanLi/SamplesInPractice/blob/master/net7Sample/Net7Sample/ArgumentExceptionSample.cs'
遠程文件這里做了一些優化,會將 Github/Gist/Gitee上的文件地址自動轉換成原始內容地址,以下兩種方式效果一樣
執行原始代碼:
dotnet-exec?'Console.WriteLine(1+1);'
執行原始腳本:
dotnet-exec?'script:1+1'
dotnet-exec?'Guid.NewGuid()'
References
執行原始代碼并自定義程序集引用:
NuGet 包引用:
dotnet-exec?'CsvHelper.GetCsvText(new[]{1,2,3}).Dump();'?-r?"nuget:?WeihanLi.Npoi,2.3.0"?-u?"WeihanLi.Npoi"
本地 dll 引用:
dotnet-exec?'CsvHelper.GetCsvText(new[]{1,2,3}).Dump();'?-r?"./out/WeihanLi.Npoi.dll"?-u?"WeihanLi.Npoi"
本地目錄下的 dll 引用:
dotnet-exec?'CsvHelper.GetCsvText(new[]{1,2,3}).Dump();'?-r?"folder:?./out"?-u?"WeihanLi.Npoi"
本地項目引用:
dotnet-exec?'CsvHelper.GetCsvText(new[]{1,2,3}).Dump();'?-r?"project:?./WeihanLi.Npoi.csproj"?-u?"WeihanLi.Npoi"
框架引用:
dotnet-exec?'WebApplication.Create().Run();'?--reference?'framework:web'
使用--web
一個選項來添加 web 框架引用:
dotnet-exec?'WebApplication.Create().Run();'?--web
一行代碼實現 web api
一行代碼使用 winform?彈出窗口
Usings
執行原始代碼并且自定義命名空間引用:
dotnet-exec?'WriteLine(1+1);'?--using?"static?System.Console"
執行原始腳本并且自定義命名空間引用:
dotnet-exec?'CsvHelper.GetCsvText(new[]{1,2,3}).Dump()'?-r?"nuget:WeihanLi.Npoi,2.4.2"?-u?WeihanLi.Npoi
其他示例
執行原始代碼并且指定更多依賴:
dotnet-exec?'typeof(LocalType).FullName.Dump();'?--ad?FileLocalType2.cs
dotnet-exec?'typeof(LocalType).FullName.Dump();'?--addition?FileLocalType2.cs
執行原始代碼并且指定從項目文件中提取 using 信息和 reference 信息:
dotnet-exec?'typeof(LocalType).FullName.Dump();'?--project?./Sample.csproj
執行本地文件并指定啟用預覽特性:
dotnet-exec?RawStringLiteral.cs?--preview
Config Profile
你可以自定義常用的配置到一個 profile 配置里以方便重復使用,使用幫助可以參考命令行幫助
列出所有可用的 profile 配置:
dotnet-exec?profile?ls
配置一個 profile:
dotnet-exec?profile?set?web?-r?"nuget:WeihanLi.Web.Extensions"?-u?'WeihanLi.Web.Extensions'?--web?--wide?false
獲取一個 profile 配置詳情:
dotnet-exec?profile?get?web
移除不需要的 profile 配置:
dotnet-exec?profile?rm?web
執行代碼時指定某一個 profile 配置:
dotnet-exec?'WebApplication.Create().Chain(_=>_.MapRuntimeInfo()).Run();'?--profile?web

dotnet-exec?'WebApplication.Create().Run();'?--profile?web?--using?'-WeihanLi.Extensions'
More
https://github.com/WeihanLi/dotnet-exec
https://www.nuget.org/packages/dotnet-execute
https://hub.docker.com/r/weihanli/dotnet-exec
https://github.com/WeihanLi/dotnet-exec/blob/main/docs/slides/dotnet-conf-china-2022-dotnet-exec_makes_csharp_more_simple.pdf