My question is based on an example from WPF, but, I think, it’s more about C# in general.
Suppose I have a WPF app where I use several types of Custom Controls, let it be CustControl1, CustControl2, CustControl3. The page can dynamically load the XAML with controls of either of the types.
This very page has a code-behind where some manipulations are made with the Custom Controls, like:
List<CustControl1> MyCustControls = this.Descendents().OfType<CustControl1>().ToList();
foreach (CustControl1 cntr in MyCustControls)
{
...
In the above code the CustControl1 type is explicitly defined, and if other types of Custom Controls are loaded on the page (of CustControl2 or CustControl3 type), the code will not recognize it.
My level of C# knowledge is insufficient to find out how to solve such a problem of multiple type recognizing. Or is it possible in C# at all?
If I understand your question correctly, this is a basic OOP concept.
You would pass in all of your controls as their parent class (UserControl, or even Control), or an interface they all implement (IControl for example)
then, if the method you are trying to call exists in the parent, you can just call it:
or, if you need to call it explicitly on concrete implementation, you can do this: