Would IsSubclassOf() work here?

  • Thread starter Thread starter Daniel Billingsley
  • Start date Start date
D

Daniel Billingsley

Given
classA {}
Type typeA = typeof(ClassA);

1) isn't

checkType.IsSubclassOf(typeA)

going to always give the same results as
typeInheritsFrom(checkType, typeA)
given
bool typeInheritsFrom(Type typeToCheck, Type checkAgainst)
{
Type baseType = typeToCheck;
while (baseType != null)
{
if(baseType == checkAgainst)
return true;
baseType = baseType.BaseType;
}
}

2) Wouldn't
if (baseType is checkAgainst)
be a little clearer in C# than
if (baseType == checkAgainst)
even though I think it would work out to the same result.
 
I was trying to write the original message too quickly I think -

I should have included the detail that checkType is always a class.
 
Daniel,
1) isn't

checkType.IsSubclassOf(typeA)

going to always give the same results as
typeInheritsFrom(checkType, typeA)

Almost. typeInheritsFrom would return true for

typeInheritsFrom(typeof(ClassA), typeof(ClassA))

but

typeof(ClassA).IsSubclassOf(typeof(ClassA))

returns false.

2) Wouldn't
if (baseType is checkAgainst)
be a little clearer in C# than
if (baseType == checkAgainst)
even though I think it would work out to the same result.

if (baseType is checkAgainst)

isn't valid code.



Mattias
 
Back
Top