Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace dispose_fxcop_test
- {
- internal class Child : IDisposable
- {
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- protected virtual void Dispose(bool disposing)
- {
- }
- ~Child()
- {
- Dispose(false);
- }
- }
- internal class Owner : IDisposable
- {
- private readonly Child _child;
- public Owner(Child child)
- {
- _child = child;
- }
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- protected virtual void Dispose(bool disposing)
- {
- if (disposing)
- {
- _child.Dispose();
- }
- }
- ~Owner()
- {
- Dispose(false);
- }
- }
- internal class Program
- {
- //todo: fix CA:2000
- private static Owner CreateOwner()
- {
- Child child = null;
- try
- {
- child = new Child();
- return new Owner(child);
- }
- catch
- {
- child?.Dispose();
- throw;
- }
- }
- private static void Main(string[] args)
- {
- using (var owner = CreateOwner())
- {
- //do something useful
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement