dotnet-exec
is a command line tool for executing C# program without a project file, and you can have your custom entry point other than Main
method
Latest stable version:
dotnet tool update -g dotnet-execute
Latest preview version:
dotnet tool update -g dotnet-execute --prerelease
Execute local file:
dotnet-exec HttpPathJsonSample.cs
Execute local file with custom entry point:
dotnet-exec 'HttpPathJsonSample.cs' --entry MainTest
Execute remote file:
dotnet-exec https://github.com/WeihanLi/SamplesInPractice/blob/master/net7Sample/Net7Sample/ArgumentExceptionSample.cs
Execute raw code:
dotnet-exec 'Console.WriteLine(1+1);'
Execute raw script:
dotnet-exec 'script:1+1'
dotnet-exec 'Guid.NewGuid()'
Execute raw code with custom references:
NuGet package reference:
dotnet-exec 'CsvHelper.GetCsvText(new[]{1,2,3}).Dump();' -r "nuget: WeihanLi.Npoi,2.3.0" -u "WeihanLi.Npoi"
Local dll reference:
dotnet-exec 'CsvHelper.GetCsvText(new[]{1,2,3}).Dump();' -r "./out/WeihanLi.Npoi.dll" -u "WeihanLi.Npoi"
Local dll in a folder references:
dotnet-exec 'CsvHelper.GetCsvText(new[]{1,2,3}).Dump();' -r "folder: ./out" -u "WeihanLi.Npoi"
Local project reference:
dotnet-exec 'CsvHelper.GetCsvText(new[]{1,2,3}).Dump();' -r "project: ./WeihanLi.Npoi.csproj" -u "WeihanLi.Npoi"
Framework reference:
dotnet-exec 'WebApplication.Create().Run();' --reference 'framework:web'
Web framework reference in one option:
dotnet-exec 'WebApplication.Create().Run();' --web
Execute raw code with custom usings:
dotnet-exec 'WriteLine(1+1);' --using "static System.Console"
Execute script with custom reference:
dotnet-exec 'CsvHelper.GetCsvText(new[]{1,2,3}).Dump()' -r "nuget:WeihanLi.Npoi,2.4.2" -u WeihanLi.Npoi
Execute with additional dependencies
dotnet-exec 'typeof(LocalType).FullName.Dump();' --ad FileLocalType2.cs
dotnet-exec 'typeof(LocalType).FullName.Dump();' --addition FileLocalType2.cs
Execute with exacting references and usings from the project file
dotnet-exec 'typeof(LocalType).FullName.Dump();' --project ./Sample.csproj
Execute file with preview features:
dotnet-exec RawStringLiteral.cs --preview
You can customize the config you used often into a config profile to reuse it for convenience.
List the profiles had configured:
dotnet-exec profile ls
Configure a profile:
dotnet-exec profile set web -r "nuget:WeihanLi.Web.Extensions" -u 'WeihanLi.Web.Extensions' --web --wide false
Get the profile details:
dotnet-exec profile get web
Remove the profile not needed:
dotnet-exec profile rm web
Executing with specific profile config:
dotnet-exec 'WebApplication.Create().Chain(_=>_.MapRuntimeInfo()).Run();' --profile web --using 'WeihanLi.Extensions'
Executing with specific profile config and remove preset specific using:
dotnet-exec 'WebApplication.Create().Run();' --profile web --using '-WeihanLi.Extensions'
Execute with 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"
for full image tag list, see https://hub.docker.com/r/weihanli/dotnet-exec/tags
- Roslyn
- NuGet.Clients
- System.CommandLine
- Thanks JetBrains for the open source Rider license
- Thanks the contributors and users for this project