'Is there something like Rust's std::option_env in csharp?

In Rust you can inspect environment variables at compile time like so:

let sha: Option<&'static str> = option_env!("CI_COMMIT_SHA");
println!("CI_COMMIT_SHA: {:?}", sha);

Is this possible now C# too, or do I really have to sed-replace the source file during the build process like back in the old days?

Is there a convenient way to evaluate environment variables during compile time in netcore/.NET5/C#9 or later nowadays?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source