GenericEnvironment is generic System.Environment class library.
Environment variables
...
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"YourIntVariable": "12345",
"YourBoolVariable": "true",
"YourStringVariable": "YourStringVariable"
...
}
...
Get variables with strict types
int yourIntVariable = GenericEnvironment.GetEnvironmentVariable<int>("YourIntVariable");
bool yourBoolVariable = GenericEnvironment.GetEnvironmentVariable<bool>("YourBoolVariable");
string yourStringVariable = GenericEnvironment.GetEnvironmentVariable<string>("YourStringVariable");
// Output
// yourIntVariable - 12345
// yourBoolVariable - true
// yourStringVariable - "YourStringVariable"
Exceptions
GenericEnvironment.GetEnvironmentVariable<bool>(null); // ArgumentNullException because name parameter is null.
GenericEnvironment.GetEnvironmentVariable<bool>("InvalidName"); // InvalidOperationException because variable not found by name.
GenericEnvironment.GetEnvironmentVariable<bool?>("YourBoolVariable"); // InvalidCastException because type is nullable (bool?)
GenericEnvironment.GetEnvironmentVariable<bool>("YourIntVariable"); // FormatException because cannot convert variable to type (bool -> int)
- GenericEnvironment.GetEnvironmentVariable
- GenericEnvironment.TryGetEnvironmentVariable
- GenericEnvironment.GetEnvironmentVariableOrDefault
C# is a strictly typed language and I want to work environment variables as strictly typed variables. This library solves this problem.
I hope this library is useful for you, if so please give a star for this repository, thank you :)