Last active
June 10, 2017 12:57
-
-
Save atsvetkov/4a5f4eeaad3396e81cdc2ead75bdc898 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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