This post is a continuation from the previous one, which shows how to get all classes that implements the IPassengerTitleStrategy and create and object of them and put them in a list which could be used to databind to a drop down list.
The following method will populate an IList whith implementations of type T.
private static IList<T> GetObjectsFrom<T>() {
Type strategyType = typeof (T);
List<Type> types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => strategyType.IsAssignableFrom(p) && p.IsClass).ToList();
return types.Select(type => (T) Activator.CreateInstance(type)).ToList();
}
This is how you can retrieve a list of objects from the classes that implements the interface IPassengerTitleStrategy:
private static void Main(string[] args) {
var types = GetObjectsFrom<IPassengerTitleStrategy>();
foreach (var passengerTitleStrategy in types) {
new Context(passengerTitleStrategy).DoAlgorithm();
}
}