Skip to content

Instantly share code, notes, and snippets.

View kcargile's full-sized avatar
🏃‍♂️
Building something rad

Kristopher Cargile kcargile

🏃‍♂️
Building something rad
View GitHub Profile
@kcargile
kcargile / IEnumerableExtensions.cs
Created August 5, 2012 14:24
.NET compare two lists for equivalence using a predicate sort key.
using System;
using System.Collections.Generic;
using System.Linq;
namespace Extensions
{
public static class IEnumerableExtensions
{
// e.g. bool sameList = list1.Equals(list2, entity => entity.Id)
@kcargile
kcargile / ReflectionUtil.cs
Created August 5, 2012 21:29
.NET get a list of types that implement a (non-specific) generic interface.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Reflection
{
public interface IGenericInterface<T>
{
@kcargile
kcargile / CachedServiceBase.cs
Created August 7, 2012 16:54
.NET templated base class that provides a caching method that will query automatically in the case of a cache miss using a function pointer.
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Caching
{
public abstract class CachedServiceBase<T> where T : class
{
private static readonly object Lock = new object();
protected ICacheProvider<T> Cache;
@kcargile
kcargile / TypeExtensions.cs
Created August 7, 2012 21:02
.NET extension method to determine if a type's superclass is of non-specific generic type.
using System;
namespace Extensions
{
public static class TypeExtensions
{
public static bool IsSubclassOfRawGeneric(this Type type, Type generic)
{
if (null == generic)
{
@kcargile
kcargile / IEnumerableExtensions.cs
Created August 7, 2012 21:07
.NET IEnumerable<T> extension method to perform LINQ where filtering only when a certain condition is met (e.g. a "where-if").
using System;
using System.Collections.Generic;
using System.Linq;
namespace Extensions
{
public static class IEnumerableExtensions
{
public static IEnumerable<TSource> WhereIf<TSource>(this IEnumerable<TSource> source, bool condition, Func<TSource, bool> predicate)
{
@kcargile
kcargile / DateTimeExtensions.cs
Created August 7, 2012 21:21
.NET approximate equality of two dates by determining if they are within one second of each other.
using System;
namespace Extensions
{
public static class DateTimeExtensions
{
public static bool ApproximatelyEqual(this DateTime t, DateTime obj)
{
return Math.Abs((t - obj).TotalSeconds) < 1;
}
@kcargile
kcargile / RowVersionConvention.cs
Created August 8, 2012 17:51
.NET Fluent nHibernate IVersionConvention for MSSQL rowversion column.
using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.Instances;
namespace Data
{
public class RowVersionConvention : IVersionConvention
{
public void Apply(IVersionInstance instance)
{
instance.Column("RowVersion");
@kcargile
kcargile / DisableADLDSPwPolicy.bat
Created August 10, 2012 20:21
Disable domain password policy for local ADLDS instances.
# script courtesy of Per Ivar Jørgensen Seterlund @ http://code.seterlund.com/2011/05/disable-password-policies-in-adlds-adam.html
dsmgmt "Configurable Settings" Connections "connect to server localhost:389" q "Set ADAMDisablePasswordPolicies to 1" "Commit changes" q q
@kcargile
kcargile / ObjectExtensions.cs
Created August 20, 2012 13:11
.NET Object extension methods to shortcut null argument checks.
using System;
namespace Extensions
{
/// <summary>
/// Contains <see cref="object"/> extension methods.
/// </summary>
public static class ObjectExtensions
{
/// <summary>
@kcargile
kcargile / IEnumerableExtensions.cs
Created September 14, 2012 17:55
.NET Converts the specified collection to an array without references to dynamically generated proxies.
namespace Extensions
{
/// <summary>
/// Extension methods for classes that implement the <see cref="IEnumerable"/> interface.
/// </summary>
public static class IEnumerableExtensions
{
/// <summary>
/// Converts the collection to an array that is guaranteed not to be proxied.
/// </summary>