Пример:
Я хотел бы иметь несколько специализированных текстовых полей, которые являются производными либо от TextBox, либо от RichTextBox, которые оба являются производными от TextBoxBase:
class CommonFeatures<T> : T where T : TextBoxBase
{
// lots of features common to the TextBox and RichTextBox cases, like
protected override void OnTextChanged(TextChangedEventArgs e)
{
//using TextBoxBase properties/methods like SelectAll();
}
}
, а затем
class SpecializedTB : CommonFeatures<TextBox>
{
// using properties/methods specific to TextBox
protected override void OnTextChanged(TextChangedEventArgs e)
{
... base.OnTextChanged(e);
}
}
и
class SpecializedRTB : CommonFeatures<RichTextBox>
{
// using methods/properties specific to RichTextBox
}
К сожалению
class CommonFeatures<T> : T where T : TextBoxBase
не компилируется («Невозможно получить из« T », потому что это параметр типа»).
Есть ли хорошее решение для этого? Спасибо.