I have an abstract class Device. A number of classes derive from it, which represent specific devices. So we have, e.g. class RedDevice : Device, class BlueDevice : Device, etc. They are all stored under the Foo.Devices namespace. Here's an overview of the structure:
- Foo
| ↳ Devices (folder)
| | ↳ RedDevice
| | ↳ BlueDevice
| ↳ Device
I want to read create a list with all devices under Foo.Devices, cast to their parent class Device. I can get a list of Types using the following code:
List<System.Type> deviceTypes =
Assembly.GetExecutingAssembly().GetTypes().Where(t => t.Namespace ==
typeof(RedDevice).Namespace).ToList();
But how can I cast them to Device? Direct cast isn't allowed by the compiler and everything else throws wrong type exceptions. Thanks!
Aucun commentaire:
Enregistrer un commentaire