Type.IsSubclassOf

M

ME

It appears that IsSubclassOf does not work. In the following example, why
does
typeof(IDerived).IsSubclassOf(typeof(IRoot));
yeild False when the IDerived interface DOES in fact derive from IRoot? The
IsSubClassOf documentation states the following:

"Determines whether the current Type derives from the specified Type"

Wouldn't an interface be a Type? Basically I want to be able to look at an
interface TYPE and determine if it is related or derived from another
Interface TYPE.

Thanks,

Matt

public interface IRoot {}
public interface IDerived : IRoot {}
public class RootClass : IRoot {}
public class DerivedClass : RootClass {}
class IsSubclassTest
{
public static void Main()
{
RootClass root = new RootClass();
DerivedClass derived = new DerivedClass();
int [] intArray = new int [10];
Console.WriteLine("Is Array a derived class of int[]? {0}",
typeof(Array).IsSubclassOf(intArray.GetType())); //False
Console.WriteLine("Is int [] a derived class of Array? {0}",
intArray.GetType().IsSubclassOf(typeof(Array))); //True
Console.WriteLine("Is IRoot a derived class of IDerived? {0}",
typeof(IRoot).IsSubclassOf(typeof(IDerived))); //False
Console.WriteLine("Is IDerived a derived class of IRoot? {0}",
typeof(IDerived).IsSubclassOf(typeof(IRoot))); //False???
Console.WriteLine("Is RootClass a derived class of DerivedClass? {0}",
root.GetType().IsSubclassOf(derived.GetType())); //False
Console.WriteLine("Is DerivedClass a derived class of RootClass? {0}",
derived.GetType().IsSubclassOf(root.GetType())); //True
Console.Read();
}
}
 
S

Sean Hederman

ME said:
It appears that IsSubclassOf does not work. In the following example, why
does
typeof(IDerived).IsSubclassOf(typeof(IRoot));
yeild False when the IDerived interface DOES in fact derive from IRoot?
The IsSubClassOf documentation states the following:

"Determines whether the current Type derives from the specified Type"

Wouldn't an interface be a Type? Basically I want to be able to look at
an interface TYPE and determine if it is related or derived from another
Interface TYPE.

Use typeof(IRoot).IsAssignableFrom(typeof(IDerived)), since this determines
if two types can be used interchangeably. IsSubclass determines if a type
derives from another, but interfaces aren't really derived, rather
implemented.

typeof(CDerived).IsSubclassOf(typeof(CRoot));

works as you expect.
Thanks,

Matt

public interface IRoot {}
public interface IDerived : IRoot {}
public class RootClass : IRoot {}
public class DerivedClass : RootClass {}
class IsSubclassTest
{
public static void Main()
{
RootClass root = new RootClass();
DerivedClass derived = new DerivedClass();
int [] intArray = new int [10];
Console.WriteLine("Is Array a derived class of int[]? {0}",
typeof(Array).IsSubclassOf(intArray.GetType())); //False
Console.WriteLine("Is int [] a derived class of Array? {0}",
intArray.GetType().IsSubclassOf(typeof(Array))); //True
Console.WriteLine("Is IRoot a derived class of IDerived? {0}",
typeof(IRoot).IsSubclassOf(typeof(IDerived))); //False
Console.WriteLine("Is IDerived a derived class of IRoot? {0}",
typeof(IDerived).IsSubclassOf(typeof(IRoot))); //False???
Console.WriteLine("Is RootClass a derived class of DerivedClass? {0}",
root.GetType().IsSubclassOf(derived.GetType())); //False
Console.WriteLine("Is DerivedClass a derived class of RootClass? {0}",
derived.GetType().IsSubclassOf(root.GetType())); //True
Console.Read();
}
}
 
M

ME

Thanks!

Matt
Sean Hederman said:
ME said:
It appears that IsSubclassOf does not work. In the following example,
why does
typeof(IDerived).IsSubclassOf(typeof(IRoot));
yeild False when the IDerived interface DOES in fact derive from IRoot?
The IsSubClassOf documentation states the following:

"Determines whether the current Type derives from the specified Type"

Wouldn't an interface be a Type? Basically I want to be able to look at
an interface TYPE and determine if it is related or derived from another
Interface TYPE.

Use typeof(IRoot).IsAssignableFrom(typeof(IDerived)), since this
determines if two types can be used interchangeably. IsSubclass determines
if a type derives from another, but interfaces aren't really derived,
rather implemented.

typeof(CDerived).IsSubclassOf(typeof(CRoot));

works as you expect.
Thanks,

Matt

public interface IRoot {}
public interface IDerived : IRoot {}
public class RootClass : IRoot {}
public class DerivedClass : RootClass {}
class IsSubclassTest
{
public static void Main()
{
RootClass root = new RootClass();
DerivedClass derived = new DerivedClass();
int [] intArray = new int [10];
Console.WriteLine("Is Array a derived class of int[]? {0}",
typeof(Array).IsSubclassOf(intArray.GetType())); //False
Console.WriteLine("Is int [] a derived class of Array? {0}",
intArray.GetType().IsSubclassOf(typeof(Array))); //True
Console.WriteLine("Is IRoot a derived class of IDerived? {0}",
typeof(IRoot).IsSubclassOf(typeof(IDerived))); //False
Console.WriteLine("Is IDerived a derived class of IRoot? {0}",
typeof(IDerived).IsSubclassOf(typeof(IRoot))); //False???
Console.WriteLine("Is RootClass a derived class of DerivedClass? {0}",
root.GetType().IsSubclassOf(derived.GetType())); //False
Console.WriteLine("Is DerivedClass a derived class of RootClass? {0}",
derived.GetType().IsSubclassOf(root.GetType())); //True
Console.Read();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top