This is a project full of utility methods that can be used across multiple projects.
The code is:
- Thoroughly documented
- Thoroughly tested
- Comprehensively null-annotated
- Includes SourceLink so you can step into the source to debug
- Methods that are side-effect free are marked as
[Pure]
This library can be installed from Nuget or Github Packages.
It targets netstandard 2.1,
and will require a version of JSON.NET installed in the 11.x
range.
You can install it from nuget by running dotnet add package SCM.SwissArmyKnife
If you are developing the library or want the latest packages built from the main
branch, you can get them from Github packages.
- Add a
nuget.config
file to the root of your project.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="github" value="https://nuget.pkg.github.com/SCADAMINDS/index.json" />
</packageSources>
<packageSourceCredentials>
<github>
<add key="Username" value="USERNAME" />
<add key="ClearTextPassword" value="TOKEN" />
</github>
</packageSourceCredentials>
</configuration>
- Replace USERNAME with your Github username and TOKEN with a personal access token.
- Add the package:
dotnet add package SCM.SwissArmyKnife --version {VERSION} --prerelease
A bunch of handy extension methods that you'll probably like to use! Examples:
// ------------- Dictionary.GetOr
myDictionary.GetOr("nonExistingKey", () => "myFallbackValue");
// ------------- Task.Select()
var enumerableTask = Task.FromResult(new int[]{1,2});
// Select to transform multiple values
// Alternative to (await enumerableTask).Select(i => i + 1)
await enumerableTask.Select(i => i + 1); // Returns [2,3]
// ------------- object.Yield
// Produce an Enumerable out of an item
int myItem = 3;
// Type: IEnumerable<int>
var myEnumerable = myItem.Yield();
// ------------- HttpClient.GetAsJsonAsync()
var client = new HttpClient();
var url = "http://www.some-url-that-produces-json.com"
// Gets URL and serializes model to MyResponseModel. On error prints http response
var await response = client.GetAsJsonAsync<MyResponseModel>(url)
Methods to operate on files:
// Get the current solution directory. It will start with the current working directory and traverse upwards until finding the solution directory.
DirectoryInfo solutionDir = FileLocator.TryGetSolutionDirectoryInfo();
// If you need to get a specific file based on a relative path from the solution directory you can do it like this.
FileInfo programFile = FileLocator.GetFileStartingAtSolutionDirectory("SourceProject", "Program.cs");
Methods to compress and decompress data using gzip:
// Will return the compressed byte array
byte[] myCompressedByteArray = Gzip.Compress(myByteArray);
// Will return the compressed string as byte array, using ASCII for encoding
byte[] myCompressedStringASCII = Gzip.Compress(myString, Encoding.ASCII);
// Will return the compressed string as byte array, using UTF8 for encoding
byte[] myCompressedStringUTF8 = Gzip.Compress(myString);
// Will return the decompressed byte array
byte[] myDecompressedByteArray = Gzip.Decompress(myCompressedByteArray);
// Will return the decompressed string using the provided encoding
string myDecompressedStringASCII = Gzip.DecompressToString(myCompressedStringASCII, Encoding.ASCII);
// Will return the decompressed string using the default (UTF8) encoding
string myDecompressedStringUTF8 = Gzip.DecompressToString(myCompressedStringUTF8);
Utilities to make testing a little bit easier.
// Create a temporary file. The file is deleted when the block ends.
using var temporaryFile = TemporaryFileFixture.Create();
FileStream fileStream = temporaryFile.FileInfo.OpenWrite();
// Create a temporary directory, and delete it recursively at the end of the block.
using var temporaryDirectory = TemporaryDirectoryFixture.Create();
var directoryPath = temporaryDirectory.DirectoryInfo.FullName
// Create a dictionary that always returns the same value: "defaultValue" in this case.
var dictionary = new SameValueDictionary<string, string>("defaultValue");
// Add operations are no-op
dictionary["someKey"] = "foo";
// Returns "defaultValue"
var value = dictionary["someKey"];
You can view the documentation for the main
branch here.
If you'd like to contribute view the contribution docs here