Skip to content

Instantly share code, notes, and snippets.

@atsvetkov
Last active June 10, 2017 12:57
Show Gist options
  • Save atsvetkov/4a5f4eeaad3396e81cdc2ead75bdc898 to your computer and use it in GitHub Desktop.
Save atsvetkov/4a5f4eeaad3396e81cdc2ead75bdc898 to your computer and use it in GitHub Desktop.
public static class Guard
{
public static void NotNull<T>(Expression<Func<T>> expression) where T : class
{
var value = /* somehow compile the expression and invoke the delegate to produce a value */;
if (value == null)
{
throw new ArgumentNullException(GetParameterName(expression));
}
}
private static string GetParameterName(Expression reference)
{
var lambda = reference as LambdaExpression;
var member = lambda.Body as MemberExpression;
return member.Member.Name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment