Skip to content

Instantly share code, notes, and snippets.

View mattfrear's full-sized avatar

Matt Frear mattfrear

View GitHub Profile
@mattfrear
mattfrear / ObjectExtensions.cs
Last active November 7, 2018 16:05
With method for setting any property on any object including read only properties (for tests)
public static class ObjectExtensions
{
public static T With<T, TProp>(this T o, Expression<Func<T, TProp>> prop, TProp value)
{
if (!(prop.Body is MemberExpression memberExpression))
{
throw new ArgumentException($"A property must be provided. ({prop})");
}
var propertyInfo = (PropertyInfo) memberExpression.Member;