Intercode.Toolbox.Core 2.1.2

dotnet add package Intercode.Toolbox.Core --version 2.1.2
NuGet\Install-Package Intercode.Toolbox.Core -Version 2.1.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Intercode.Toolbox.Core" Version="2.1.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Intercode.Toolbox.Core --version 2.1.2
#r "nuget: Intercode.Toolbox.Core, 2.1.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Intercode.Toolbox.Core as a Cake Addin
#addin nuget:?package=Intercode.Toolbox.Core&version=2.1.2

// Install Intercode.Toolbox.Core as a Cake Tool
#tool nuget:?package=Intercode.Toolbox.Core&version=2.1.2

Intercode.Toolbox.Core

A trimmable, AOT-compatible .NET library that provides general purpose functionality created for several projects over the years.

Classes

AssemblyExtensions

Provides extension methods for working with assemblies.

  • GetVersionString: Gets the version string of the assembly; will return the assembly's informational version if the AssemblyInformationalVersionAttribute is found.
        var version = Assembly.GetExecutingAssembly().GetVersionString();
        Console.WriteLine( version );
    

BaseConverter

Provides methods for converting numbers between different number bases. It supports bases from 2 to 62, unlike the built-in System.Convert.ToString method, which only supports the 2, 8, 10 or 16 bases.

  • ConvertToString<T> Converts the specified value to a string representation in the given radix.

        var value = 1234567890;
        var base62 = BaseConverter.ConvertToString( value, 62 );
        Console.WriteLine( base62 );
    
  • ConvertFromString<T> Converts the specified string representation in the given radix to a value.

        var base62 = "2lkCB1";
        var value = BaseConverter.ConvertFromString<int>( base62, 62 );
        Console.WriteLine( value );
    
  • TryConvertFromString<T> Tries to convert the specified string representation in the given radix to a value.

        var base62 = "2lkCB1";
        if( BaseConverter.TryConvertFromString<int>( base62, 62, out var value ) )
        {
            Console.WriteLine( value );
        }
    

Disposer

Disposer will invoke a user-provided action when disposed.

    using( new Disposer( static () => Console.WriteLine( "Disposed" ) ) )
    {
        Console.WriteLine( "Using Disposer" );
    }

EmailAddressListAttribute

Represents a custom validation attribute that validates a collection of email addresses.

    public class MyModel
    {
      [EmailAddressList]
      public IEnumerable<string> EmailAddresses { get; set; } = new List<string>();
    }

EmbeddedResource

Provides utility methods for loading embedded resources.

  • LoadBytesFromResource Loads the content of an embedded resource as a byte array.
    var imageBytes = EmbeddedResource.LoadBytesFromResource( "logo.png" );

-LoadFromResourceLoads the content of an embedded resource as a stream along with its content type.

    var (stream, contentType) = EmbeddedResource.LoadFromResource( "logo.png" );
    Console.WriteLine( contentType );

IntegerEncoder

Uses multiplicative inverses to obfuscate ids and avoid exposing sequential values that could potentially be exploited. The algorithm is not cryptographically strong, but is very fast and cheap. It is suitable for obfuscating ids in URLs and similar scenarios. The range of values that can be encoded is between -1,000,000,000 and -1,000,000,000.

  • Encode Encodes the specified integer value.
    var encoded = IntegerEncoder.Encode( 42 );
    Console.WriteLine( encoded ); // 543321076
  • Decode Decodes the specified encoded value.
    var decoded = IntegerEncoder.Decode( 543321076 );
    Console.WriteLine( decoded ); // 42

Mime

Provides methods for working with MIME types. The list of mappings can be extended by adding new entries to the Mime.Mappings dictionary.

GetContentType Gets the content type based on the file name.

    var contentType = Mime.GetContentType( "sample.txt" );
    Console.WriteLine( contentType ); // text/plain

ObjectExtensions

Provides extension methods for the System.Object class.

  • IsNumber Determines whether the specified object is a number.
    object obj = 42;
    Console.WriteLine( obj.IsNumber() ); // True

    obj = "Hello, World!";
    Console.WriteLine( obj.IsNumber() ); // False

PhoneListAttribute

Represents a custom validation attribute that validates a list of phone numbers.

    public class MyModel
    {
      [PhoneList]
      public IEnumerable<string> PhoneNumbers { get; set; } = new List<string>();
    }

ReaderWriterLockExtensions

Extension methods for the System.Threading.ReaderWriterLockSlim class.

  • ReadLock Acquires a read lock on the specified ReaderWriterLockSlim object. The lock is released when the returned IDisposable object is disposed.
    using( readerWriterLock.ReadLock() )
    {
        // Read operation
    }
  • WriteLock Acquires a write lock on the specified ReaderWriterLockSlim object. The lock is released when the returned IDisposable object is disposed.
    using( readerWriterLock.WriteLock() )
    {
        // Write operation
    }
  • UpgradeableReadLock Acquires an upgradeable read lock on the specified ReaderWriterLockSlim object. The lock is released when the returned IDisposable object is disposed.
    using( readerWriterLock.UpgradeableReadLock() )
    {
        // Upgradeable read operation

        using ( readerWriterLock.WriteLock() )
        {
            // Write operation
        }

        // Upgradeable read operation
    }

StreamExtensions

Provides extension methods to read and write values to and from a Sytem.Stream object.

  • ToByteArray Reads the entire stream and returns its content as a byte array. It has an optimization for MemoryStream where it behaves exactly as calling ToArray().
    using( var stream = new MemoryStream( new byte[] { 1, 2, 3, 4, 5 } ) )
    {
        var bytes = stream.ToByteArray();
        Console.WriteLine( string.Join( ", ", bytes ) ); // 1, 2, 3, 4, 5
    }

` 'ToByteArrayAsync' Asynchronously converts a stream to a byte array

    await using( var stream = new MemoryStream( new byte[] { 1, 2, 3, 4, 5 } ) )
    {
        var bytes = await stream.ToByteArrayAsync();
        Console.WriteLine( string.Join( ", ", bytes ) ); // 1, 2, 3, 4, 5
    }

StringExtensions

Provides extension methods for System.String objects.

  • RemoveDiacritics Removes diacritics from the specified string.
    var text = "Héllö, Wörld!";
    var normalized = text.RemoveDiacritics();
    Console.WriteLine( normalized ); // Hello, World!

UriBuilderExtensions

Provides extension methods for System.UriBuilder instances.

  • AppendPath Appends a path segment to the URI builder's path; ensures that the appended path value is URL encoded.
    var builder = new UriBuilder( "https://example.com/" );
    builder.AppendPath( "<api>" );
    Console.WriteLine( builder.Uri ); // https://example.com/%3Capi%3E
  • AppendQuery Appends a query parameter to the URI builder's query; ensures that the appended query key is URL encoded.
    var builder = new UriBuilder( "https://example.com/" );
    builder.AppendQuery( "<key>" );
    Console.WriteLine( builder.Uri ); // https://example.com/?%3Ckey%3E
  • AppendQuery Appends a query parameter with a value to the URI builder's query; ensures that the appended query key and value are URL encoded.
    var builder = new UriBuilder( "https://example.com/" );
    builder.AppendQuery( "<key>", "<value>" );
    Console.WriteLine( builder.Uri ); // https://example.com/?%3Ckey%3E=%3Cvalue%3E

VariableIntegerEncoder

Provides methods for encoding and decoding integers using the Variable Length Quantity (VLQ) encoding scheme. This method is used in several protocols to encode integers in a compact form, such as MIDI and Google Protocol Buffers. Supports encoding sbyte, byte, short, ushort, and int values. The maximum required buffer size is 5 bytes.

  • TryEncode Tries to encode the specified integer value into a span of bytes.
    Span<byte> buffer = stackalloc byte[5];
    if( VariableIntegerEncoder.TryEncode( 42, buffer, out var bytesWritten ) )
    {
        Console.WriteLine( string.Join( ", ", buffer.Take( bytesWritten ) ) ); // 42
    }
  • Encode Encodes the specified integer value into a byte array.
    var encoded = VariableIntegerEncoder.Encode( int.MaxValue );
    Console.WriteLine( string.Join( ", ", encoded ) ); // 255, 255, 255, 255, 7
  • Decode Decodes the specified byte span into an integer value.
    var buffer = new byte[] { 255, 255, 255, 255, 7 };
    var decoded = VariableIntegerEncoder.Decode( buffer );
    Console.WriteLine( decoded ); // 2147483647
  • WriteEncoded Writes the encoded integer value to the specified Stream object.
    using( var stream = new MemoryStream() )
    {
        VariableIntegerEncoder.WriteEncoded( stream, int.MaxValue );
        Console.WriteLine( string.Join( ", ", stream.ToArray() ) ); // 255, 255, 255, 255, 7
    }
  • ReadEncoded Reads an encoded integer value from the specified Stream object.
    using( var stream = new MemoryStream( new byte[] { 255, 255, 255, 255, 7 } ) )
    {
        var decoded = VariableIntegerEncoder.ReadEncoded( stream );
        Console.WriteLine( decoded ); // 2147483647
    }

Usage

To use the extension methods provided by this project, simply import the Intercode.Toolbox.Core namespace and call the desired method on the target object.

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.1.2 38 7/3/2024