abstract class CustomControl : UserControl
{
protected abstract int DoStuff();
}
class DetailControl : CustomControl
{
protected override int DoStuff()
{
// do stuff
return result;
}
}
我在表单中放置了一个DetailControl。运行时它可以正确地呈现,但是设计师显示出错并且因为基础用户控件是抽象的无法打开。
目前,我正在考虑以下补丁,这似乎对我来说非常不正确,因为我想让子类被迫实现该方法。
class CustomControl : UserControl
{
protected virtual int DoStuff()
{
throw new InvalidOperationException("This method must be overriden.");
}
}
class DetailControl : CustomControl
{
protected override int DoStuff()
{
// do stuff
return result;
}
}
有没有更好的方法来解决这个问题?