How do you prevent IDisposable from spreading to all your classes?

后端 未结 7 677
抹茶落季
抹茶落季 2020-12-07 10:28

Start with these simple classes...

Let\'s say I have a simple set of classes like this:

class Bus
{
    Driver busDriver = new Driver();
}

class          


        
7条回答
  •  北荒
    北荒 (楼主)
    2020-12-07 10:49

    This is basically what happens when you mix Composition or Aggregation with Disposable classes. As mentioned, the first way out would be to refactor the waitHandle out of shoelace.

    Having said that, you can strip down the Disposable pattern considerably when you don't have unmanaged resources. (I'm still looking for an official reference for this.)

    But you can omit the destructor and GC.SuppressFinalize(this); and maybe cleanup the virtual void Dispose(bool disposing) a bit.

提交回复
热议问题