问题描述:

I have a List of Components

List<Component> components = new List<Component>();

I am designing a dynamic forms framework. I know that casting to a specific component is impossible even if I know what kind of component each of them is. How can I get the component as a specific type (for example TextBox, ComboBox) in order to use their properties.

For example I know that the first item of components is a ComboBox.

Can I get it as ComboBox in order to use it?

网友答案:

Use Enumerable.OfType

Filters the elements of an IEnumerable based on a specified type.

var textBoxes = components.OfType<TextBox>();
相关阅读:
Top