find out whether there already is an object of the same derived typein a generic collection

  • Thread starter Thread starter Fabian
  • Start date Start date
F

Fabian

Hello,

I have a class hierarchy of "Task Activity" classes for a machine
control system. To manage the activities I have a dictionary class,
derived from Dictionary<string, TaskActivity>.
I have to check whether this dictionary (already) contains a
TaskActivity object of a specific type and - now it becomes difficult -
the mother class hierarchy.

If it was just the same Type my method below would work:

public bool ContainsObjectOfType(Type ElementType)
{
foreach (TaskActivity activity in this.Values)
{
if (activity.GetType() == ElementType)
return true;
}
return false;
}

I e.g. call it via
bool result =
TaskActivityDictionary.ContainsObjectOfType(typeof(MotherClassOfTaskActivity));

activity.GetType() == ElementType is only true if the object is of
exactly the same class but I need to know whether it is derived of the
mother class or the mother's mother class... (therefore
activity.GetType().BaseType doesn't work).

Is there another possibility of comparing types at runtime?

Cheers,

Fabian
 
Back
Top