Skip to content

Instantly share code, notes, and snippets.

View davefancher's full-sized avatar

Dave Fancher davefancher

View GitHub Profile
public static T SetProperty<T, U>(
this T @this,
Expression<Func<T, U>> propertyExpression,
U value)
{
propertyExpression
.Body
.ConvertTo<MemberExpression>()
.Member
.Name
@davefancher
davefancher / Game.cs
Created September 9, 2016 17:34
Fun with delegates - a math game (LINQPad)
/*
# Math Game
http://www.umassmed.edu/bsrc/tricks/#days1998
## Instructions
1. Pick the number of days a week that you would like to go out (1 - 7).
2. Multiply this number by 2.
3. Add 5.
4. Multiply the new total by 50.
@davefancher
davefancher / ParseHelper.cs
Last active July 28, 2016 17:40
Some helper functions for streamlining TryParse
public delegate bool Parser<T>(string s, out T result);
public class ParseResult<T>
{
internal ParseResult(bool success, T value)
{
Success = success;
Value = value;
}
// LINQPad Example
// Updated from http://davefancher.com/2010/04/16/linq-ienumerable-to-datatable/
// Defines an extension method that creates a new System.Data.DataTable from an IEnumerable<T>
// Even works with anonymous types
public static class IEnumerableExtensions
{
public static DataTable ConvertToDataTable<TSource>(this IEnumerable<TSource> source)
{
var props = typeof(TSource).GetProperties();