code_junkie

Is it possible in .Net to call some code only from debug builds of client assemblies?

Nov 13th, 2011
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. public static class Ensures {
  2. public static Validation That {
  3. get { ... }
  4. }
  5. }
  6.  
  7. public static class Requires {
  8. public static Validation That {
  9. get { ... }
  10. }
  11. }
  12.  
  13. public static class Ensures {
  14. [ConditionalCallingCode("DEBUG")]
  15. public static Validation ThatDuringDebug {
  16. get { ... }
  17. }
  18. }
  19.  
  20. public class Foo {
  21. public void Bar() {
  22. ... // do some work
  23. Ensures.That // do these checks always
  24. .IsNotNull(result)
  25. .IsInRange(result, 0, 100);
  26.  
  27. Ensures.WhileDebuggingThat // only do these checks in debug mode
  28. .IsPositive(ExpensiveCalculation(result));
  29.  
  30. return result;
  31. }
  32. }
  33.  
  34. public class Foo {
  35. public void Bar() {
  36. ... // do some work
  37. Ensures.That // do these checks always
  38. .IsNotNull(result)
  39. .IsInRange(result, 0, 100);
  40.  
  41. #ifdef DEBUG
  42. Ensures.That // only do these checks in debug mode
  43. .IsPositive(ExpensiveCalculation(result));
  44. #endif
  45.  
  46. return result;
  47. }
  48. }
  49.  
  50. using ValidationLibrary;
  51. public static class EnsuresWhileDebugging {
  52. [Conditional("DEBUG")]
  53. public static Validation That() {
  54. return Ensures.That;
  55. }
  56. }
  57.  
  58. Ensures.That.IsPositive(ExpensiveCalculation(result));
  59.  
  60. Ensures.WhileDebugging.That. IsPositive(() => ExpensiveCalculation(result));
  61.  
  62. try
  63. {
  64. Debug.Assert(false);
  65. }
  66. catch (Exception e)
  67. {
  68. // will only and always run in debug mode
  69.  
  70. }
  71.  
  72. Debug.Assert(!Properties.Settings.Default.UseAutoDebug);
Add Comment
Please, Sign In to add comment