Intercode.Toolbox.Core
2.1.4
See the version list below for details.
dotnet add package Intercode.Toolbox.Core --version 2.1.4
NuGet\Install-Package Intercode.Toolbox.Core -Version 2.1.4
<PackageReference Include="Intercode.Toolbox.Core" Version="2.1.4" />
<PackageVersion Include="Intercode.Toolbox.Core" Version="2.1.4" />
<PackageReference Include="Intercode.Toolbox.Core" />
paket add Intercode.Toolbox.Core --version 2.1.4
#r "nuget: Intercode.Toolbox.Core, 2.1.4"
#:package Intercode.Toolbox.Core@2.1.4
#addin nuget:?package=Intercode.Toolbox.Core&version=2.1.4
#tool nuget:?package=Intercode.Toolbox.Core&version=2.1.4
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 theAssemblyInformationalVersionAttributeis 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.
Base 62 is useful for generating shorter identifiers that are URL-safe and case-insensitive. It uses the characters 0-9, a-z, and A-Z.
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.
LoadBytesFromResourceLoads 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 identifiers 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 identifiers in URLs and similar scenarios; additional
encoding with the BaseConverter using base 62 can be used for further obfuscation.
The range of values that can be encoded is between -1,000,000,000 and -1,000,000,000.
EncodeEncodes the specified integer value.
var encoded = IntegerEncoder.Encode( 42 );
Console.WriteLine( encoded ); // 543321076
DecodeDecodes 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.
IsNumberDetermines 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.
ReadLockAcquires a read lock on the specifiedReaderWriterLockSlimobject. The lock is released when the returnedIDisposableobject is disposed.
using( readerWriterLock.ReadLock() )
{
// Read operation
}
WriteLockAcquires a write lock on the specifiedReaderWriterLockSlimobject. The lock is released when the returnedIDisposableobject is disposed.
using( readerWriterLock.WriteLock() )
{
// Write operation
}
UpgradeableReadLockAcquires an upgradeable read lock on the specifiedReaderWriterLockSlimobject. The lock is released when the returnedIDisposableobject 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.
ToByteArrayReads the entire stream and returns its content as a byte array. It has an optimization forMemoryStreamwhere it behaves exactly as callingToArray().
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.
RemoveDiacriticsRemoves 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.
AppendPathAppends 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
AppendQueryAppends 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
AppendQueryAppends 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.
TryEncodeTries 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
}
EncodeEncodes the specified integer value into a byte array.
var encoded = VariableIntegerEncoder.Encode( int.MaxValue );
Console.WriteLine( string.Join( ", ", encoded ) ); // 255, 255, 255, 255, 7
DecodeDecodes 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
WriteEncodedWrites the encoded integer value to the specifiedStreamobject.
using( var stream = new MemoryStream() )
{
VariableIntegerEncoder.WriteEncoded( stream, int.MaxValue );
Console.WriteLine( string.Join( ", ", stream.ToArray() ) ); // 255, 255, 255, 255, 7
}
ReadEncodedReads an encoded integer value from the specifiedStreamobject.
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 | Versions 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Microsoft.Extensions.FileProviders.Embedded (>= 8.0.7)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Intercode.Toolbox.Core:
| Package | Downloads |
|---|---|
|
Intercode.Toolbox.AspNetCore.Extensions
A trimmable, AOT-compatible .NET library that contains types that provide functionality commonly used in ASP.NET Core applications. |
GitHub repositories
This package is not used by any popular GitHub repositories.