ParamGuard.cs 973 B

12345678910111213141516171819202122232425262728293031
  1. using System;
  2. namespace Wayne.Lib
  3. {
  4. public static class ParamGuard
  5. {
  6. public static void AssertIsNotNull<T>(T paramValue, string paramName) where T : class
  7. {
  8. if (paramValue == null)
  9. {
  10. throw new ArgumentNullException(paramName);
  11. }
  12. }
  13. public static void AssertMeetsCondition<T>(T paramValue, Func<T, bool> paramChecker, string paramName, string conditionName)
  14. {
  15. if (!paramChecker(paramValue))
  16. {
  17. throw new ArgumentException(string.Format("Parameter {0} does not meet condition {1} ", paramName, conditionName));
  18. }
  19. }
  20. public static void AssertNotNullOrEmpty(string paramValue, string paramName)
  21. {
  22. if (string.IsNullOrEmpty(paramValue))
  23. {
  24. throw new ArgumentException(string.Format("Parameter {0} is null or empty ", paramName));
  25. }
  26. }
  27. }
  28. }