I have an interface list which stores a variety of objects derived from that interface. I would like to write a function where the caller specifies the type to extract. I’ve tried this:
List<IParts> PartList;
…
public List<IPart> Fetch(Type PartType)
{
return this.PartList.OfType<PartType>().Cast<IPart>().ToList();
}
But it doesn’t like a passed type. Any suggestions?
If you have to support various types at runtime (rather than at compile time with generics):