Advertisement
Guest User

Untitled

a guest
Jul 1st, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. using System;
  2.  
  3. namespace dispose_fxcop_test
  4. {
  5. internal class Child : IDisposable
  6. {
  7. public void Dispose()
  8. {
  9. Dispose(true);
  10. GC.SuppressFinalize(this);
  11. }
  12.  
  13. protected virtual void Dispose(bool disposing)
  14. {
  15. }
  16.  
  17. ~Child()
  18. {
  19. Dispose(false);
  20. }
  21. }
  22.  
  23. internal class Owner : IDisposable
  24. {
  25. private readonly Child _child;
  26.  
  27. public Owner(Child child)
  28. {
  29. _child = child;
  30. }
  31.  
  32. public void Dispose()
  33. {
  34. Dispose(true);
  35. GC.SuppressFinalize(this);
  36. }
  37.  
  38. protected virtual void Dispose(bool disposing)
  39. {
  40. if (disposing)
  41. {
  42. _child.Dispose();
  43. }
  44. }
  45.  
  46. ~Owner()
  47. {
  48. Dispose(false);
  49. }
  50. }
  51.  
  52. internal class Program
  53. {
  54. //todo: fix CA:2000
  55. private static Owner CreateOwner()
  56. {
  57. Child child = null;
  58. try
  59. {
  60. child = new Child();
  61. return new Owner(child);
  62. }
  63. catch
  64. {
  65. child?.Dispose();
  66. throw;
  67. }
  68. }
  69.  
  70. private static void Main(string[] args)
  71. {
  72. using (var owner = CreateOwner())
  73. {
  74. //do something useful
  75. }
  76. }
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement