I asking for a more smooth and nice way to do this (and/or corrections if I’m wrong here),
HashSet<ISomething> itemRows;
List<ISomething> PopulateItemRows()
{
itemRows = new HashSet<ISomething>();
itemRows.UnionWith(new SomeType1().Collection());
itemRows.UnionWith(new SomeType2().Collection());
itemRows.UnionWith(new SomeType3().Collection());
itemRows.UnionWith(new SomeType4().Collection());
return itemRows.ToList();
}
SomeTypeXX are all implements the ISomething.
The very best would of course be avoiding the explicit including of types.
There can be a scenario of new implementations and this method missed to be updated.
If you want a generic way to find all types implementing
ISomething:The code assumes that all types implementing
ISomethinglive in the same assembly as the interface.Update: Added some sanity checks based on Martins answer