I am trying to implement IComponent rather than derive from Component becuase I wish to turn a (non-component) base class into a component (.NET does not support multiple inheritance - if it did I could simply derive from Component and my base class). However I can't overcome the "The base class'<base class name>'cannot be designed" error when I double-click the component and launch the default component designer. Please, does anyone know how to do this?
The following code sample is my implementation so far (for sake of simplicity the example does not explicitly derive from a base class - it implicitly derives from System.Object).
Component1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace Example
{
[System.ComponentModel.DesignerCategory("Component")]
public partial class Component1 : IComponent
{
private ISite m_Site;
private bool m_Disposed;
public Component1()
{
InitializeComponent();
}
public Component1(IContainer container)
{
container.Add(this);
InitializeComponent();
}
~Component1()
{
this.Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (!this.m_Disposed)
{
if (disposing)
{
/* release managed resources */
if (components != null)
components.Dispose();
}
/* release native resource */
this.m_Disposed = true;
if (this.Disposed != null)
this.Disposed(this, EventArgs.Empty);
}
}
#region IComponent Members
public event EventHandler Disposed;
public ISite Site
{
get { return m_Site; }
set { m_Site = value; }
}
#endregion
#region IDisposable Members
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}
Component1.Designer.cs
namespace Example
{
partial class Component1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}
}