A collection of common utilities and extension methods.
This is the place for all "common" helpers, extension methods and utilities. There is only one condition: The helpers may not require any third party packages.
AsyncHelper
Run async method in a synchonous context.RandomHelper
Useful randomizing methods.TempPathHelper
Create temp files or folders safely.Disposable
Base class that implementsIDisposable
correctly.
SortedList
IList<T>
implementation that inserts the items sorted using binary search.
There are a lot of extensions methods available. here we sort them by the type they are implemented on.
IsIndexWithin
Checks if the given index is within the array.ToMemoryStream
Creates aMemoryStream
from a byte array.
GetResourceAsStringAsync
Gets the content of the given embedded resource location as string.
Repeat
Repeats the givenchar
the specified number of times.
AddIfNotContains
Adds item(s) to collection if they are not already in the collection.IsNullOrEmpty
Checks if the collection is null or empty.RemoveMatching
Removes all items from the collection that match a given predicate.
The custom interfaces IConverter
and ITryConverter
provide a fluent interface for
converting strings to numeric, boolean and Guid values.
string value = "5";
int result = value.Convert().ToInt();
string value = "5";
bool success = value.TryConvert().ToInt(out int result);
char value = '5';
int result = value.Convert().ToInt();
char value = '5';
bool success = value.TryConvert().ToInt(out int result);
The Convert()
extension methods also provide overloads to provide a custom IFormatProvider
. The default format provider is CultureInfo.CurrentCulture
.
AddMonthsToEndOfMonth
Adds the months to end of month.AddWeeks
Adds an amount of weeks to a date value.CalculateAge
Calculates the age based on a passed reference date.GetDayCountOfYear
Get the number of days within that date year.GetDays
Get the number of days between two dates.GetDaysOfMonthCount
Returns the number of days in the month of a provided date.GetFirstDayOfMonth
Returns the first day of the month of a provided date.GetFirstDayOfWeek
Gets the first day of the week using a specific culture.GetLastDayOfMonth
Returns the last day of the month of a provided date.GetLastDayOfWeek
Gets the last day of the week using a specific culture.GetNextWeekday
Gets the next occurrence of the specified weekday.GetPeriodOfDay
Returns the period of the dayPeriodOfDay.Morning
,PeriodOfDay.Afternoon
,PeriodOfDay.Evening
.GetPreviousWeekday
Gets the previous occurrence of the specified weekday.GetWeekOfYear
Gets the week number for a date time value based on the current culture settings.GetWeeksWeekday
Gets the next occurrence of a specified weekday within the current week using a specific culture.IsDateEqual
Determines if the date part of twoDateTime
values are equal.IsLeapYear
Returns if or not aDateTime
is during a leap year.IsTimeEqual
Determines if the time part of twoDateTime
values are equal.IsToday
Determines if a date isDateTime.Today
.IsWeekday
Determines if aDateTime
orDayOfWeek
is a weekday.IsWeekend
Determines if aDateTime
orDayOfWeek
is a weekend.Midnight
Gets aDateTime
representing midnight on a specified date.NextMonth
Gets the next month.Noon
Gets aDateTime
representing noon on a specified date.OrdinalSuffix
Converts the day of a date to an ordinal string, f.e. 3 to '3rd'.SetTime
Sets the time of the aDateTime
with a specified precicion. All other parts are set to zero.ToUnixTimestamp
Converts aDateTime
to a UNIX timestamp (seconds since 01.01.1970).
ContainsValue
Returnstrue
if the value is contained in a dictionary.GetOrAdd
Gets a value from a dictionary with given key. Returns a default value if the value cannot be found.GetOrDefault
Gets a value from a dictionary. Returnsdefault
if the value cannot be found.TryGetValue
Tries to get a value from a dictionary.
All operations utilize a cache of the enums metadata that is built lazily when using the extensions methods.
EnumOptions
Gets all options of an enum type.EnumValues
Gets all values of an enum type.HasFlag
A generic version ofEnum.HasFlag(enum)"
IsEmpty
Gets a flag, indicating if the enum has no values.MaxValue
Gets the maximum value of the enum.MinValue
Gets the minimum value of the enum.
AsReadOnly
Converts an enumerable to a read-only collection.ForEach
Executes anAction
for every item in the enumerable.IsAny
Checks if a enumerable is not null or empty.IsNullOrEmpty
Checks if the enumerable is null or empty.
IsEmpty
Checks aGuid
isGuid.Empty
.
AddFor
Adds a value to a list if the value matches a predicate.AddRange
Adds values to a list (IList<T>
).AddRangeFor
Adds values to a list if the value to add matches a predicate.AddRangeSorted
Adds values into a list sorted.AddSorted
Adds a value into a list sorted.
ToContentString
Converts the content of the memory stream to a string.
IsNull
Checks if a nullable has no value.
The numeric extensions methods are available for the following primitives: byte
, short
, int
, long
IsBetween
Determines if a value is between the specified values a and b (inclusive).IsEven
Determines whether a value is even.IsOdd
Determines whether a value is odd.Ordinalize
Ordinalize a number, f.e. 6 to '6th'.Times
Performs a specified action n times based on the value.
GetDefaultValue
Gets the type default value for a type. In case of reference types:null
and for values typesdefault(T)
.IsNull
Returnstrue
, if the specified target reference is equal to the null reference.Locking
Executes anAction
by locking on a given object.
GetClaimValue
Gets the first value of the given claim type and returns its value.GetClaimValues
Gets all values of the given claim type and returns their values.GetDisplayName
Gets the display name from theClaimsIdentity
or uses the subject.GetSubjectId
Gets the subject identifier.IsAuthenticated
Determines whether a principal is authenticated.
DefinesAttribute
Returnstrue
if aMemberInfo
instance is marked with the specified attribute.GetMemberType
Gets the underlying type of aMemberInfo
instance.IsIndexer
Determines if aPropertyInfo
is an indexer.IsProperty
Determines if aMethodInfo
is a property.IsPropertyGetter
Determines if aMethodInfo
is a property getter.IsPropertySetter
Determines if aMethodInfo
is a property setter.
CopyTo
Copies aStream
into a another one.CopyToMemory
Copies aStream
into a localMemoryStream
.GetBytes
Gets all bytes of aStream
.GetReader
Opens aStreamReader
for aStream
.GetWriter
Opens aStreamWriter
for aStream
.Rewind
Rewinds (set the current position to 0) aStream
.Write
Writes all bytes to the aStream
.
ConcatWith
Concatenates a string with additional strings and an optional delimiter character.EnsureEndsWith
Ensures that a string ends with a given suffix.EnsureStartsWith
Ensures that a string starts with a given prefix.FormatInvariantWith
Applies formatting to a string using the invariant culture.FormatWith
Applies formatting to a string using the current culture.FromBase64
Decodes a string from base64 to its normal representation.GetBytes
Converts a string to abyte[]
using a specific encoding.IsNullOrEmpty
Determines if a string isnull
orstring.Empty
.- **
IsNullOrWhiteSpace
**Determines if a string isnull
orstring.Empty
or consists of whitespace-only characters. IsNumeric
Checks if the contents of a string is a numeric value.Left
Gets a substring of a string from beginning of the string.RemovePostFix
Removes the first occurrence of postfixes from the end of a string.RemovePrefix
Removes the first occurrence of prefixes from the beginning of a string.RemoveSpaces
Removes all whitespace (not line endings, etc.) from a string.RemoveSpecialCharacters
Removes all special characters from a string.Repeat
Repeats a string value as provided by the repeat count.ReplaceDiacritics
Replaces all diacritical marks with an ascii representation, fe. 'ü' to 'u'.ReplaceWithEmpty
Replaces all occurrences of a string in an input string withstring.Empty
.- **
Reverse
**Reverses the a string. Right
Gets a substring of a string from end of the string.Split
Usesstring.Split(string[], StringSplitOptions)
method to split a string by a separator.ToBase64
Encodes string to base64.ToEnum
Converts a string to a matching enum value of a given enum type.ToMemoryStream
Converts a string to aMemoryStream
.Truncate
Truncates a string to a specified length and optionally replaces the truncated part with '...' suffix.UrlSafeDecode
Encodes a string for safe usage in URLs, f.e. for password reset or email confirmation tokens.UrlSafeEncode
Decodes a given URL-safe string.
Pluralize
Returns the plural form of a word.Singularize
Returns the singular form of a word.Titleize
Return a string in a form that can be used as title.Humanize
Humanizes a string.Pascalize
Pascalizes a string.Camelize
Camelizes a string.Underscore
Replaces all dashes with underscores.Capitalize
Capitalizes a string.Uncapitalize
Uncapitalizes a string.Ordinalize
Ordinalizes the given number that is given as string, f.e. '6' to '6th'.Dasherize
Converts all underscores to dashes.
CreateInstance
Creates an instance of a type.DefinesAttribute
Returnstrue
if the type is marked with a specified attribute.GetBaseClasses
Gets all base classes recursively for a type.Implements
Returnstrue
if type implements a specified interface.IsAssignableTo
Checks if a type is assignable to another type.IsCollection
Determines if the a type is a collection.IsEnumerable
Determines if the a type is an enumerable.IsNullable
Determines if the a type is a nullable.IsNumeric
Determines if the a type is numeric.IsPrimitive
Determines whether a type is a primitive.UnwrapNullableType
Gets the type without nullable if the type is aNullable<T>
.
GetSubDomain
Gets the sub-domain part from aUri
.
IsDefault
Determines if a value is empty (default(T)
).IsNullable
Determines if a type is a nullable.ToNullable
onverts a value to a corresponding nullable type.
A collection of custom data annotations.
[CompositeIndex]
- An attribute to provide the composite indices of an entity.
- Targets classes.
- Allows multiple.
[DateOnly]
- A marker attribute to signal potential data stores to only store the date.
- Targets properties and fields.
- Denys multiple.
[DatePrecision]
- An attribute to provide the date precision to potential data stores.
- Targets properties and fields.
- Denys multiple.
[Ignore]
- A marker attribute to signal potential data stores to ignore this value.
- Targets properties and fields.
- Denys multiple.
[Index]
- An attribute to provide an index of the entity.
- Targets properties and fields.
- Denys multiple.
[Reference]
- An attribute to signal potential data stores that this property should be stored as database reference.
- Targets properties and fields.
- Denys multiple.
[Contains]
- A validation that checks if the annotated property contains the given check value.
- Targets properties and fields.
- Denys multiple.
[EndsWith]
- A validation attribute that checks if the value end with the given value.
- Targets properties and fields.
- Denys multiple.
[EnforceTrue]
- A validation attribute that checks if the value is
true
. - Targets properties and fields.
- Denys multiple.
- A validation attribute that checks if the value is
[ListLength]
- A validation attribute to check the length of a list.
- Targets properties and fields.
- Denys multiple.
[ListMaxLength]
- A validation attribute to check the maximum length of a list.
- Targets properties and fields.
- Denys multiple.
[ListMinLength]
- A validation attribute to check the minimum length of a list.
- Targets properties and fields.
- Denys multiple.
[NotEmpty]
- A validation attribute to check if an enumerable contains at least one element.
- Targets properties and fields.
- Denys multiple.
[RequiredIf]
- A validation attribute that checks if the value is required when a condition is met.
- Targets properties and fields.
- Denys multiple.
[StartsWith]
- A validation attribute that checks if the value starts with the given value.
- Targets properties and fields.
- Denys multiple.
[Currency]
- A data-type attribute for currency values.
- Targets properties, fields and parameters.
- Denys multiple.
[Date]
- A data-type attribute for date values.
- Targets properties, fields and parameters.
- Denys multiple.
[DateTime]
- A data-type attribute for date-time values.
- Targets properties, fields and parameters.
- Denys multiple.
[Duration]
- A data-type attribute for duration (timespan) values.
- Targets properties, fields and parameters.
- Denys multiple.
[ImageUrl]
- A data-type attribute for image-url values.
- Targets properties, fields and parameters.
- Denys multiple.
[MultilineText]
- A data-type attribute for multiline text values.
- Targets properties, fields and parameters.
- Denys multiple.
[Password]
- A data-type attribute for password values.
- Targets properties, fields and parameters.
- Denys multiple.
[PostalCode]
- A data-type attribute for postal code values.
- Targets properties, fields and parameters.
- Denys multiple.
[Select]
- A data-type attribute to signal that the values comes from a selection of values.
- Targets properties, fields and parameters.
- Denys multiple.
[Text]
- A data-type attribute for text values.
- Targets properties, fields and parameters.
- Denys multiple.
[Time]
- A data-type attribute for time values.
- Targets properties, fields and parameters.
- Denys multiple.
[Upload]
- A data-type attribute for upload values.
- Targets properties, fields and parameters.
- Denys multiple.
A collection of LINQ expression utilities and extension methods.
The extenions methods use several utility classes that are publicly available through this library to be used forother use-cases:
Evaluator
Enables the partial evaluation of queries.LocalCollectionExpander
Expands local collection values.ParameterRebinder
Used for rebinding of parameters.
The utility classes are used to evaludate as much as possible to create a string representation of an Expression
.
The result of the extension can f.e. be used to create cache keys for expression-based data queries.
Expression<Func<Person, string>> expression = x => x.Name;
string? str = expression.ToExpressionString();
The other extension methods can be used to compose given expressions.
And
Creates anExpression
that represents an bitwise AND operation.AndAlso
Creates anExpression
that represents a conditional AND operation that evaluates the second operand only if it has to.Or
Creates anExpression
that represents an bitwise OR operation.OrElse
Creates anExpression
that represents a conditional OR operation that evaluates the second operand only if it has to.