I've been struggling with a problem for a little while now, and I'm not sure if it has an actual solution.
I've been trying to write a function that iterates through a list of a certain object type and returns only the objects that are (or inherit from) another type.
public List<Entity> getEntities(Type t)
{
List<Entity> returnList = new List<Entity>();
foreach (Entity e in entityList)
{
if (e is t)
{
returnList.Add(e);
}
}
return returnList;
}
This is the code I've been working with. The problem is that is doesn't work with variables, and any other solution that I've found does not check inheritance, which leads me to believe that this can't be done on runtime.
Is there a better way to do what I'm attempting, or do I need to find an altogether different solution?
Thanks!
Use a Type.IsAssignableFrom method to check inheritance. https://msdn.microsoft.com/en-us/library/system.type.isassignablefrom(v=vs.110).aspx