Intercode.Toolbox.Core
2.1.7
See the version list below for details.
dotnet add package Intercode.Toolbox.Core --version 2.1.7
NuGet\Install-Package Intercode.Toolbox.Core -Version 2.1.7
<PackageReference Include="Intercode.Toolbox.Core" Version="2.1.7" />
<PackageVersion Include="Intercode.Toolbox.Core" Version="2.1.7" />
<PackageReference Include="Intercode.Toolbox.Core" />
paket add Intercode.Toolbox.Core --version 2.1.7
#r "nuget: Intercode.Toolbox.Core, 2.1.7"
#:package Intercode.Toolbox.Core@2.1.7
#addin nuget:?package=Intercode.Toolbox.Core&version=2.1.7
#tool nuget:?package=Intercode.Toolbox.Core&version=2.1.7
Intercode.Toolbox.Core
A trimmable, AOT-compatible .NET library that provides general purpose functionality created for several projects over the years.
Updates
- Version 2.1.6 - Added the
PathBuilderclass for building file and directory paths using a fluent interface. - Version 2.1.7 - Added a default constructor to the
PathBuilderclass.
AssemblyExtensions
Methods
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
Methods
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.
Usage
using( new Disposer( static () => Console.WriteLine( "Disposed" ) ) )
{
Console.WriteLine( "Using Disposer" );
}
Example output:
Using Disposer
Disposed
EmailAddressListAttribute
Represents a custom validation attribute that validates a collection of email addresses. The email validation is performed using the EmailAddressAttribute class.
Usage
public class MyModel
{
[EmailAddressList]
public IEnumerable<string> EmailAddresses { get; set; } = new List<string>();
}
EmbeddedResource
Provides utility methods for loading embedded resources.
Methods
LoadBytesFromResourceLoads the content of an embedded resource as a byte array.
var imageBytes = EmbeddedResource.LoadBytesFromResource( "logo.png" );
-LoadFromResource Loads 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.
Methods
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.
Methods
GetContentTypeGets 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.
Methods
IsNumberDetermines whether the specified object is a number.
object obj = 42;
Console.WriteLine( obj.IsNumber() ); // True
obj = "Hello, World!";
Console.WriteLine( obj.IsNumber() ); // False
PathBuilder
Provides a fluent interface for building file and directory paths.
Usage
var path = new PathBuilder(@"c:\temp\myfile.tmp")
.ChangeFilename( filename => $"{DateTime.Now:yyMMdd}-{filename}" )
.Build();
Console.WriteLine( path ); // c:\temp\240820-myfile.tmp
Methods
SetDirectorysets or replaces the path's directory; ifnullis provided, the directory is removed.ChangeDirectorychanges the path's directory using a transformation function, which receives the path's current directory.AppendDirectoryappends a directory to the path.AppendDirectoriesappends one or more directories to the path.SetFilenamesets or replaces the path's filename; ifnullis provided, the filename is removed.ChangeFilenamechanges the path's filename using a transformation function, which receives the path's current filename.SetExtensionsets or replaces the path's extension; ifnullis provided, the extension is removed.ChangeExtensionchanges the path's extension using a transformation function, which receives the path's current extension.Buildbuilds the final path string.
PhoneListAttribute
Represents a custom validation attribute that validates a list of phone numbers; the validation is performed using the PhoneAttribute class.
Usage
public class MyModel
{
[PhoneList]
public IEnumerable<string> PhoneNumbers { get; set; } = new List<string>();
}
ReaderWriterLockExtensions
Extension methods for the System.Threading.ReaderWriterLockSlim class.
Methods
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.
Methods
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
}
ToByteArrayAsyncAsynchronously 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.
Methods
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
}
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.