Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving classes not objects in an array

Tags:

c#

bool InContext(Class[] array, Item item)
{
  for(int i = 0;i<array.length;i++)
   {
     if(item is array[i])
     {
        return true;
     }
   } 
   return false;
}

Is there anyway to store a bunch of classes in an array, so that I can tell if one of my items is inheriting from it?

I want this function for checking if an item is within context (for context sensitive menus) to be fairly dynamic. so writing it in manually isn't really an option.

like image 598
Sol Snare Avatar asked Aug 31 '26 19:08

Sol Snare


1 Answers

Use Type instead of 'Class' in your header line.

Then you can have:

bool InContext(Item item, Type[] accepted)
{
  Type itemType = item.GetType();

  return accepted.Any(x=> x.IsAssignableFrom(itemType));
}
like image 157
h.alex Avatar answered Sep 02 '26 10:09

h.alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!