`dotnet-exec` is a command-line tool for excuting C# program with custom entry point
$ dotnet add package dotnet-executedotnet-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.csExecute file with preview features:
dotnet-exec RawStringLiteral.cs --previewExecute raw code:
dotnet-exec 'code:Console.WriteLine(1+1);'
dotnet-exec 'Console.WriteLine(1+1);'Execute raw code with custom usings:
dotnet-exec 'code:WriteLine(1+1);' --using "static System.Console"
dotnet-exec 'WriteLine(1+1);' --using "static System.Console"Execute raw code with custom references:
NuGet package reference:
dotnet-exec 'code:CsvHelper.GetCsvText(new[]{1,2,3}).Dump();' -r "nuget: WeihanLi.Npoi,2.3.0" -u "WeihanLi.Npoi"Local dll reference:
dotnet-exec 'code:CsvHelper.GetCsvText(new[]{1,2,3}).Dump();' -r "./out/WeihanLi.Npoi.dll" -u "WeihanLi.Npoi"Local dll in a folder references:
dotnet-exec 'code:CsvHelper.GetCsvText(new[]{1,2,3}).Dump();' -r "folder: ./out" -u "WeihanLi.Npoi"Local project reference:
dotnet-exec 'code:CsvHelper.GetCsvText(new[]{1,2,3}).Dump();' -r "project: ./WeihanLi.Npoi.csproj" -u "WeihanLi.Npoi"Execute script:
dotnet-exec 'script:1+1'
dotnet-exec '1+1'Execute script with custom reference:
dotnet-exec 'script:Console.WriteLine(CsvHelper.GetCsvText(new[]{1,2,3}))' -r "nuget:WeihanLi.Npoi,2.4.2" -u WeihanLi.Npoidotnet-exec 'CsvHelper.GetCsvText(new[]{1,2,3}).Dump()' -r "nuget:WeihanLi.Npoi,2.4.2" -u WeihanLi.NpoiExecute with additional dependencies
dotnet-exec 'typeof(LocalType).FullName.Dump();' --ad FileLocalType2.csdotnet-exec 'typeof(LocalType).FullName.Dump();' --addition FileLocalType2.csExecute with exacting references and usings from project file
dotnet-exec 'typeof(LocalType).FullName.Dump();' --project ./Sample.csprojExecute 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"By default, it's using the latest language version, you can use the Preview version with --preview
By default, it would use MainTest as the entry point, you can customize with --entry option
By default, it would use net7.0 if you've installed .NET 7 SDK, otherwise use .NET 6 instead, you can customize with the -f/--framework option