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

后端 未结 7 634
抹茶落季
抹茶落季 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:57

    Interestingly if Driver is defined as above:

    class Driver
    {
        Shoe[] shoes = { new Shoe(), new Shoe() };
    }
    

    Then when Shoe is made IDisposable, FxCop (v1.36) does not complain that Driver should also be IDisposable.

    However if it is defined like this:

    class Driver
    {
        Shoe leftShoe = new Shoe();
        Shoe rightShoe = new Shoe();
    }
    

    then it will complain.

    I suspect that this is just a limitation of FxCop, rather than a solution, because in the first version the Shoe instances are still being created by the Driver and still need to be disposed somehow.

    0 讨论(0)
提交回复
热议问题