abstract class CustomControl : UserControl
{
protected abstract int DoStuff();
}
class DetailControl : CustomControl
{
protected override int DoStuff()
The following is a generic solution that works for me, mostly. It is based on the article from another answer. Sometimes it will work, and I can design my UserControl, and then later I'll open the file and it will give the "The designer must create an instance of type 'MyApp.UserControlBase' but it cannot because the type is declared as abstract." I think I can fix it by cleaning, closing VS, reopening VS, and rebuilding. Right now it seems to be behaving. Good luck.
namespace MyApp
{
using System;
using System.ComponentModel;
///
/// Replaces a class of with a class of
/// during design. Useful for
/// replacing abstract s with mock concrete
/// subclasses so that designer doesn't complain about trying to instantiate
/// abstract classes (designer does this when you try to instantiate
/// a class that derives from the abstract .
///
/// To use, apply a to the
/// class , and instantiate the attribute with
/// SwitchTypeDescriptionProvider) .
///
/// E.g.:
///
/// [TypeDescriptionProvider(typeof(ReplaceTypeDescriptionProvider))]
/// public abstract class T
/// {
/// // abstract members, etc
/// }
///
/// public class TReplace : T
/// {
/// // Implement 's abstract members.
/// }
///
///
///
///
/// The type replaced, and the type to which the
/// must be
/// applied
///
///
/// The type that replaces .
///
class ReplaceTypeDescriptionProvider : TypeDescriptionProvider
{
public ReplaceTypeDescriptionProvider() :
base(TypeDescriptor.GetProvider(typeof(T)))
{
// Nada
}
public override Type GetReflectionType(Type objectType, object instance)
{
if (objectType == typeof(T))
{
return typeof(TReplace);
}
return base.GetReflectionType(objectType, instance);
}
public override object CreateInstance(
IServiceProvider provider,
Type objectType,
Type[] argTypes,
object[] args)
{
if (objectType == typeof(T))
{
objectType = typeof(TReplace);
}
return base.CreateInstance(provider, objectType, argTypes, args);
}
}
}