Skip to content

Instantly share code, notes, and snippets.

View manisero's full-sized avatar

Michał Aniserowicz manisero

View GitHub Profile
@manisero
manisero / CeilingOfDivisionBy.cs
Last active March 15, 2019 09:59
Przebij to, Kazimierczak.
public static int CeilingOfDivisionBy(
this int value,
int divisor)
{
var intQuotient = value / divisor;
var remainder = value % divisor;
return remainder == 0
? intQuotient
: intQuotient + 1;
@manisero
manisero / Random.NextUniqueSet.cs
Last active October 20, 2018 12:09
Method generating set of unique random numbers.
/// <summary>Generates set of unique random numbers from 1 to specified max value.</summary>
public static ISet<int> NextUniqueSet(
this Random random,
int count,
int maxValueInclusive)
{
// Taken from https://stackoverflow.com/a/2394292/10401390
if (maxValueInclusive < count)
{
@manisero
manisero / IDbConnectionResolver.cs
Last active September 16, 2018 18:26
DbConnection resolver for both transaction and no-transaction scenarios.
// using System;
// using System.Data.Common;
public interface IDbConnectionResolver
{
IDbConnectionWrapper Resolve();
}
public interface IDbConnectionWrapper : IDisposable
{
@manisero
manisero / MethodInfo_InvokeAsGeneric.cs
Last active September 14, 2018 18:04
Extension methods enabling convenient generic methods invocation through reflection.
// using System;
// using System.Reflection;
// using System.Runtime.ExceptionServices;
[System.Diagnostics.DebuggerStepThrough]
public static void InvokeAsGeneric(
this MethodInfo method,
object target,
Type[] typeArguments,
params object[] arguments)
@manisero
manisero / ThreadSafeCache.cs
Created November 23, 2017 22:08
Simple thread-safe cache spending minimum time in lock (thanks to Lazy).
public class ThreadSafeCache<TKey, TItem>
{
private readonly ConcurrentDictionary<TKey, Lazy<TItem>> _cache = new ConcurrentDictionary<TKey, Lazy<TItem>>();
private readonly object _lock = new object();
public TItem GetOrAdd(TKey key, Func<TKey, TItem> itemFactory)
{
Lazy<TItem> item;
if (!_cache.TryGetValue(key, out item))
@manisero
manisero / LightLazy.cs
Created November 23, 2017 22:03
Light, allocation-free (but not thread-safe) Lazy implementation.
public static class LightLazy
{
public static LightLazy<TItem> Create<TItem>(Func<TItem> itemConstructor)
where TItem : class
=> new LightLazy<TItem>(itemConstructor);
}
public struct LightLazy<TItem>
where TItem : class
{
{
"$type": "Manisero.DSLExecutor.Domain.ExpressionsDomain.BatchExpression`1[[System.Int32, mscorlib]], Manisero.DSLExecutor",
"SideExpressions": {
"$type": "Manisero.DSLExecutor.Domain.ExpressionsDomain.IExpression[], Manisero.DSLExecutor",
"$values": [
{
"$type": "Manisero.DSLExecutor.Domain.ExpressionsDomain.FunctionExpression`2[[Manisero.DSLExecutor.Parser.Json.Tests.TestsDomain.LogFunction, Manisero.DSLExecutor.Parser.Json.Tests],[Manisero.DSLExecutor.Domain.FunctionsDomain.Void, Manisero.DSLExecutor]], Manisero.DSLExecutor",
"FunctionType": "Manisero.DSLExecutor.Parser.Json.Tests.TestsDomain.LogFunction, Manisero.DSLExecutor.Parser.Json.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
"ArgumentExpressions": {
"$type": "System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[Manisero.DSLExecutor.Domain.ExpressionsDomain.IExpression, Manisero.DSLExecutor]], mscorlib",
public static Type GetGenericTypeDefinitionImplementation(this Type type, Type definition)
{
if (definition.IsInterface)
{
return type.GetGenericInterfaceDefinitionImplementation(definition);
}
else
{
return type.GetGenericClassDefinitionImplementation(definition);
}
@manisero
manisero / .gitconfig
Last active January 24, 2023 08:28
Useful Git aliases and other options. Feel free to copy them to your "C:\Users\[user]\.gitconfig" file (Windows).
[core]
longpaths = true
[http]
sslVerify = false
[credential]
helper = manager-core
[pull]
rebase = true
[push]
default = simple