Skip to content

Instantly share code, notes, and snippets.

@lisardggY
lisardggY / Fluent Cast
Last active May 16, 2019 09:15
For casting an object as part of a Fluent expression without having to put annoying parentheses around casting expressions.
public static T As<T>(this object objectToCast)
{
return objectToCast is T typedObject ? typedObject : default;
}
// Create new object to cache iframe offsets
$.ui.ddmanager.frameOffsets = {};
// Override the native `prepareOffsets` method. This is almost
// identical to the un-edited method, except for the last part!
$.ui.ddmanager.prepareOffsets = function (t, event) {
var i, j,
m = $.ui.ddmanager.droppables[t.options.scope] || [],
type = event ? event.type : null, // workaround for #2317
list = (t.currentItem || t.element).find(":data(ui-droppable)").addBack(),
@lisardggY
lisardggY / ToHexBytes.cs
Created November 25, 2015 06:40
Encodes a string into a base16/hexadecimal string format. Useful for maintaining case sensitivity where case sensitivity cannot be maintained, like a filename or URL.
using System.Text;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
public static string ToHexBytes(this string plainText, Encoding encoding = null)
{
encoding = encoding ?? Encoding.UTF8;
var bytes = encoding.GetBytes(plainText);
return new SoapHexBinary(bytes).ToString();
}
public static string FromHexBytes(this string hexText, Encoding encoding = null)
@lisardggY
lisardggY / AsSingletonCollection.cs
Created November 26, 2015 09:01
Find yourself wrapping single objects in arrays or lists to pass to multi-value APIs? Tired of writing new [] {myObject}; all the time? These extension methods will make your collection-wrapping fluent and painless.
public static T[] AsSingletonArray<T>(this T single)
{
return new[] {single};
}
public static List<T> AsSingletonList<T>(this T single)
{
return new List<T> {single};
}
@lisardggY
lisardggY / UpcastTask.cs
Created February 3, 2016 13:12
Upcast a Task<DerivedClass> to a Task<BaseClass>
public static async Task<TBase> Upcast<TDerived, TBase> (this Task<TDerived> task) where TDerived : TBase
{
return (TBase) await task;
}
@lisardggY
lisardggY / PointDistanceExtension.cs
Created March 23, 2016 08:52
Calculate distance between two points
public static class PointExtensions
{
public static double DistanceFrom(this Point point1, Point point2)
{
// Pythagorean distance calculation.
return Math.Abs(
Math.Sqrt(
Math.Pow(point1.X - point2.X, 2) + Math.Pow(point1.Y - point2.Y, 2)
)
);
@lisardggY
lisardggY / SelectIfNotNull.cs
Created July 14, 2016 13:31
SelectIfNotNull - Like Select, but returns only those mapped results that aren't null. Equivalent to items.Select().Where(x => x != null)
public static IEnumerable<U> SelectIfNotNull<T, U>(this IEnumerable<T> source, Func<T, U> mapper)
{
foreach (var sourceItem in source)
{
var resultItem = mapper(sourceItem);
if (resultItem != null)
{
yield return resultItem;
}
}
@lisardggY
lisardggY / FindInTree.cs
Created July 14, 2016 13:32
FindInTree - recursively searches a tree of objects. Expects as parameters a lambda to retrieve an item's identifier and a lambda to get a node's children.
/// <summary>
/// Recursively searches a tree structure for a specific node.
/// </summary>
/// <typeparam name="TChild">The node's type.</typeparam>
/// <typeparam name="TIdentifier">The node identifier's type</typeparam>
/// <param name="root">The root node to search under.</param>
/// <param name="identifier">The identifier of the node to find.</param>
/// <param name="childSelector">A function that returns a node's children in the tree.</param>
/// <param name="identifierSelector">A function that returns a node's identifier</param>
/// <returns>Either a <cref>TChildType</cref> instance whose identifier matches the one specified, or null if not found.</returns>
@lisardggY
lisardggY / Selectable<T>.cs
Created September 28, 2016 12:50
Selectable view model
public class Selectable<T> : ViewModelBase
{
private bool _isSelected;
public Selectable(T item)
{
Item = item;
IsSelected = false;
}
@lisardggY
lisardggY / Release.cs
Created May 9, 2017 08:27
Mark Task as fire-and-forget
/// <summary>
/// Signals the compiler that this task should be allowed to run independently.
/// </summary>
/// <param name="task"></param>
public static void Release(this Task task)
{
}