something.GetType() == typeof(someOther)
checks whether the type of
something
is
equal to the type of
someOther
. If you want to check whether
something
inherits from
someOther
then use the keyword
is
:
if (f is classC)
{
}
Edit: In case the types are equal,
is
will also return true (so not only for actually derived types).
Edit 2: As PIEBALDConsult has pointed out in his solution, there are alternatives: Type.IsAssignableFrom(..) and Type.IsInstanceOfType(..) that do (mostly) the same thing as the keyword
is
. Which one is most convenient to use depends on what you have to compare to what (an instance of an object / a type object / a static type). See here:
http://stackoverflow.com/questions/15853159/isassignablefrom-isinstanceoftype-and-the-is-keyword-what-is-the-difference[
^]