Skip to content

Instantly share code, notes, and snippets.

View jnm2's full-sized avatar
🧬
Evolving

Joseph Musser jnm2

🧬
Evolving
View GitHub Profile
@jnm2
jnm2 / StackEnumerator.cs
Created May 2, 2015 20:18
Gives foreach recursive abilities without recursive calls
using System;
using System.Collections.Generic;
using System.ComponentModel;
public sealed class StackEnumerator<T> : IDisposable
{
private readonly Stack<IEnumerator<T>> stack = new Stack<IEnumerator<T>>();
private IEnumerator<T> current;
public bool MoveNext()
@jnm2
jnm2 / DerivedDiscriminatorConverter.cs
Last active September 12, 2015 18:53
Provides a fluent API to map discriminator property values to derived types and handle both serialization and deserialization.
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
public sealed class DerivedDiscriminatorConverter<TBase> : JsonConverter where TBase : class
{
private readonly string discriminatorProperty;
private readonly Dictionary<object, Type> typeByDiscriminator = new Dictionary<object, Type>();
public static class LanguageUtils
{
public static TInterface ExplicitImplementation<TBase, TInterface>(TBase @this)
where TBase : TInterface
where TInterface : class
{
return (TInterface)new ExplicitImplementationProxy(typeof(TBase), @this).GetTransparentProxy();
}
private sealed class ExplicitImplementationProxy : RealProxy, IRemotingTypeInfo
@jnm2
jnm2 / PasswordEdit.cs
Last active January 9, 2018 03:55
Hooks into the key messages to a DevExpress TextEdit at a low level and replaces all characters with '*', maintaining a separate SecureString to store the actual password.
/// <summary>
/// Hooks into the key messages at a low level and replaces all characters with '*', maintaining a separate SecureString to store the actual password.
/// </summary>
[UserRepositoryItem("Register")]
public sealed class RepositoryItemPasswordEdit : RepositoryItemTextEdit
{
static RepositoryItemPasswordEdit()
{
Register();
}
@jnm2
jnm2 / DerivedDiscriminatorModelBinder.cs
Last active August 29, 2015 14:23
Provides a fluent API to map discriminator property values to derived types and handle MVC model deserialization.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
public class DerivedDiscriminatorModelBinder<T> : DefaultModelBinder where T : class
{
private readonly string discriminatorPropertyName;
private readonly Dictionary<object, Type> mapping = new Dictionary<object, Type>();
@jnm2
jnm2 / ObjectPropertyBinder.cs
Created June 16, 2015 13:24
This class provides dead simple automatic property notifications for calculated and proxy properties, even if the source properties do not report changes.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Techsola.Threading;
@jnm2
jnm2 / ExpressionComparer.cs
Created June 16, 2015 18:06
Compares two abstract syntax trees.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
public sealed class ExpressionComparer : IEqualityComparer<Expression>
{
public NameComparison CompareLambdaNames { get; set; }
public NameComparison CompareParameterNames { get; set; }
@jnm2
jnm2 / SubstringGetHashCode.cs
Last active August 29, 2015 14:24
Calculates the String.GetHashCode compatible hash code for a substring without allocating the substring
/// <summary>
/// Calculates the hash code for a substring without allocating the substring.
/// </summary>
// Identical to String.GetHashCode except for custom start and stop
// I have unit tests on this
[SecuritySafeCritical]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
public static int GetHashCode(this string str, int startIndex, int length)
{
if (str == null) throw new ArgumentNullException("str");
@jnm2
jnm2 / AsyncSimulatorExperimental.cs
Last active May 7, 2018 19:38
Simulates an await by running the message pump for the current thread, but does not return until the task ends. Same pattern as ShowDialog().
// #define ASYNC_SIMULATOR_RAISE_IDLE - Disabled due to conflict with XtraReportEx.CreateDocument(true) and AsyncWaitData, and no known benefit
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
using System.Threading;
@jnm2
jnm2 / WeakEventSource.cs
Last active August 27, 2019 07:40
Replaces a multicast delegate as an event's backing store. Duplicate behavior, except it is thread safe and holds weak references.
// MIT license, copyright 2015 Joseph N. Musser II
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
namespace jnm2
{