9

Is this the simplest way to determine if foo is the same or derived from type T

bool Derives<T>(object foo)
{
  return foo is T;
}

and an exact match would be

bool ExactMatch<T>(object foo)
{
  return foo.GetType() == typeof(T);
}
maxp
  • 24,209
  • 39
  • 123
  • 201
  • 1
    Does the answer have to take interfaces into account (e.g. is `ArrayList` derived from `ICollection`? Is `ICollection` derived from `IEnumerable`?)? Does it have to take generics into account? If so, how should it handle the generic type parameters (e.g. is `List` derived from `IList`? Is `List` derived from `List`?)? – Jon May 10 '11 at 10:54
  • ExactMatch won't work because of `RuntimeType` issues – Daniel May 10 '11 at 11:00
  • 1
    RuntimeType issues such as... ? – Antony Woods May 10 '11 at 11:06

1 Answers1

6

I can't think of a simpler way :)

(and in 'answer' format, to please the trolls: "Yes")

Antony Woods
  • 4,415
  • 3
  • 26
  • 47