#!/usr/bin/dotnet Console.WriteLine("Hello .NET");
我们也可以使用在文件中指定 #property PublishAot=false 来不 publish aot,也可以 publish 的时候指定比如 :
dotnet publish hello.cs -p PublishAot=false
#:project ../Net10Samples Net10Samples.JsonSamples.JsonIgnoreWhenReadWriteSample();
# 1. Create a single-file C# app with a shebang cat << 'EOF' > hello.cs #!/usr/bin/env dotnet Console.WriteLine("Hello!"); EOF # 2. Copy it (extensionless) into ~/utils/hello (~/utils is on my PATH) mkdir -p ~/utils cp hello.cs ~/utils/hello # 3. Mark it executable chmod +x ~/utils/hello # 4. Run it directly from anywhere cd ~ hello
#:property LangVersion=preview Console.WriteLine("From [CallerFilePath] attribute:"); Console.WriteLine($" - Entry-point path: {Path.EntryPointFilePath()}"); Console.WriteLine($" - Entry-point directory: {Path.EntryPointFileDirectoryPath()}"); Console.WriteLine("From AppContext data:"); Console.WriteLine($" - Entry-point path: {AppContext.EntryPointFilePath()}"); Console.WriteLine($" - Entry-point directory: {AppContext.EntryPointFileDirectoryPath()}"); staticclassPathEntryPointExtensions { extension(Path) { public static string EntryPointFilePath() => EntryPointImpl(); public static string EntryPointFileDirectoryPath() => Path.GetDirectoryName(EntryPointImpl()) ?? ""; private static string EntryPointImpl([System.Runtime.CompilerServices.CallerFilePath] string filePath = "") => filePath; } } staticclassAppContextExtensions { extension(AppContext) { publicstaticstring? EntryPointFilePath() => AppContext.GetData("EntryPointFilePath") asstring; publicstaticstring? EntryPointFileDirectoryPath() => AppContext.GetData("EntryPointFileDirectoryPath") asstring; } }