In this article I’ll discuss about the criteria behind choosing IDisposable, IComponent, Component, MarshalByValueComponent and base Control classes(System.Windows.Form.Control and System.Web.UI.Control) while implementing a Class.
Prior to discussing further I’d like to share the reason behind this article. I ran code analysis in a project code and got a warning that Dispose was not called for a DataSet in the project’s code. I than called the Dispose method for that particular DataSet and this time code analysis executed with zero errors/warnings. Out of curiosity I thought of verifying the use of calling Dispose on DataSet as even though when Dispose was not being called everything was fine. I found that calling Dispose method did nothing as DataSet is a managed object and does not contain unmanaged resources(if I’m wrong please correct me on this). I could verify this as I was able to access/update the DataSet even after Dispose had been called without any “Object Disposed Exception”. FeatureSchema is a typed DataSet as displayed in code snippet below
FeatureSchema fs = Proxy.GetFeatureSchema();
foreach (FeatureSchema.FeatureRow featureRow in fs.Feature.Rows)
//Do procesing on dataset
public partial class FeatureSchema : global::System.Data.DataSet
//Added only a destructor to this types dataset. I have excluded all other code
// as I am focusing on finalizers
//I am just loading the data into this typed dataset
FeatureSchema schema = new FeatureSchema();
If a class uses external resources and will not be used on a design surface IDisposable interface has to be implemented directly or indirectly e.g. System.Drawing.Font class etc.
If a class will be used on a design surface IComponent interface has be implemented directly or indirectly. IComponent implements IDisposable e.g. System.Web.UI.Control class etc.
If a class will be used on a design surface and is Marshalled by reference then it has to derive from Component class e.g. System.Timers.Timer class etc.
If a class will be used on a design surface and is Marshalled by value then it has to derive from MarshalByValueComponent class e.g. DataSet, DataTable etc.
If a class will be used on a design surface and provides a user interface then this class is a control and has to derive from System.Windows.Form.Control or System.Web.UI.Control e.g. System.Windows.Forms.Button etc.
The above discussion does not apply to WPF. The WPF Designer architecture is significantly different from the Windows Forms Designer architecture, which is characterized by the IComponent interface and the System.ComponentModel namespace. The WPF Designer architecture retains the TypeConverter and TypeDescriptor classes from the Windows Forms Designer object model. Most other aspects of the WPF Designer architecture are different. For more information please read Comparing the Windows Forms Designer Framework to the WPF Designer Framework