Created
November 14, 2011 09:39
-
-
Save MioGreen/1363616 to your computer and use it in GitHub Desktop.
Requires<T> pattern
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 Contract | |
{ | |
public static void Requires<T>(Func<bool> condition) where T : Exception, new() | |
{ | |
if(!condition.Invoke()) | |
{ | |
throw new T(); | |
} | |
} | |
} | |
#Example of using | |
public class A | |
{ | |
public A(params IB[] bs) | |
{ | |
Contract.Requires<ArgumentException>(() => bs != null); | |
... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment