tags:

views:

322

answers:

3

I started messing around with FXCop against some of my assemblies and noticed something interesting that I'm trying to wrap my head around.

"Types that declare disposable members should also implement IDisposable. If the type does not own any unmanaged resources, do not implement a finalizer on it."

I have my own class that contains some controls that in turn inherit from System.Web.UI.WebControls.Button. It's not complaining about my inherited buttons, but the class that wraps them is.

What's the impact or danger here?

+2  A: 

If your class contains IDisposable types, but your class is not IDisposable, then it is possible that the IDisposable types within your class are not disposed / freed on time, when an instance of your class is no longer needed. Dispoble types should be disposed as soon as you no longer need them, so that resources are freed. If you do not do that, you'll have to wait until the GC kicks in, then the resources will be freed as well.

Frederik Gheysels
+1  A: 

As you know, when you finish using a disposable object, you should call its Dispose method.

When you inherit from these controls, it is still possible to call the Dispose method. But if you make a wrapper, then the user of your wrapper class should be able to call Dispose.

public void Dispose() {
    button.Dispose();
    // any other thing that is disposable
}
configurator
+1  A: 

What's the nature of the class that contains the buttons?

Normally I would only expect a class that's a composite control to have other controls as members. Such a class would normally inherit directly or indirectly from System.Web.UI.CompositeControl or System.Web.UI.WebControl, and the controls you have as members should be added to the Controls collection.

Doing so will ensure they are disposed properly and should get rid of the FxCop warning.

Post more details of the class in question if this doesn't help.

Joe
Excellent point, I think I'll go that route. Thanks!
Chris