What is the best way to solve the following problem?
foreach (Control control in this.Controls)
{
if (control is ComboBox || control is TextBox)
{
ComboBox controlCombobox = control as ComboBox;
TextBox controlTextbox = control as TextBox;
AutoCompleteMode value = AutoCompleteMode.None;
if (controlCombobox != null)
{
value = controlCombobox.AutoCompleteMode;
}
else if (controlTextbox != null)
{
value = controlTextbox.AutoCompleteMode;
}
// ...
}
}
You see it's complicated enough to get the AutoCompleteMode-property. You can assume that it is guaranteed that I have either a ComboBox or a TextBox.
My first idea was to use generic with multiple types for T, but it seems that this is not possible in .NET:
public string GetAutoCompleteModeProperty<T>(T control) where T: ComboBox, TextBox // this does not work, of course
Sadly the both Controls don't have a common base class.
Note: This is meant to be a more general question used with a minimized example. In my case, I also want to access / manipulate other the AutoComplete*-proprties (which also both Controls have in common).
Thanks for ideas!