dotnet-exec 让 C# 程序更简单

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

6bf8c6f3372b5e7c53429800b018932b.png

工作流程分为三步:

  • 获取代码:目前支持本地代码、远程代码以及原始代码

  • 代码编译:Roslyn 完成代码动态编译

  • 代码执行:基于 AssemblyLoadContext 的代码执行

核心实现是基于 Roslyn 来完成动态编译ff3373407a9e02d3ac47e6ca35396713.png

编译的时候分成三种情况

  • 一种是代码是有Main方法的Console应用,直接执行 Main 方法即可

  • 一种是没有 Main 方法的 DLL,需要自定义入口方法,执行自定义的入口方法

  • 最后是Script代码是由Roslyn的Scripting功能进行支持进行编译和执行

    abb100e3f0e54bf804967bf1fd72a91b.png

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

详细示例:

8d7da27a0fd62061a56d3a3a61528f2d.png

执行远程文件:

dotnet-exec 'https://github.com/WeihanLi/SamplesInPractice/blob/master/net7Sample/Net7Sample/ArgumentExceptionSample.cs'

远程文件这里做了一些优化,会将 Github/Gist/Gitee上的文件地址自动转换成原始内容地址,以下两种方式效果一样

336bfeb5059fb2422c9e0f5c2b16b095.png

执行原始代码:

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

82244485b7ee178afdf72e5d2cea2957.png

一行代码使用 winform 弹出窗口cf149015c25b41519cd2a34e4ae0369f.png

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

c8c788cc036f34f21614f63e8ac84e24.png

其他示例

执行原始代码并且指定更多依赖:

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 配置里以方便重复使用,使用帮助可以参考命令行帮助311d914bee52299a77360b3d3ead9cbd.png

列出所有可用的 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

d08444e0bb5a94a45e0bd758a45436e8.png执行代码时指定某一个 profile 配置:

dotnet-exec 'WebApplication.Create().Chain(_=>_.MapRuntimeInfo()).Run();' --profile web
910f6cd7376203a7aa039a52aed6ddab.png 执行代码时指定某一个 profile 配置并且移除配置中的某一个 using:
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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值