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.WriteLi ne("Is Array a derived class of int[]? {0}",
typeof(Array).I sSubclassOf(int Array.GetType() )); //False
Console.WriteLi ne("Is int [] a derived class of Array? {0}",
intArray.GetTyp e().IsSubclassO f(typeof(Array) )); //True
Console.WriteLi ne("Is IRoot a derived class of IDerived? {0}",
typeof(IRoot).I sSubclassOf(typ eof(IDerived))) ; //False
Console.WriteLi ne("Is IDerived a derived class of IRoot? {0}",
typeof(IDerived ).IsSubclassOf( typeof(IRoot))) ; //False???
Console.WriteLi ne("Is RootClass a derived class of DerivedClass? {0}",
root.GetType(). IsSubclassOf(de rived.GetType() )); //False
Console.WriteLi ne("Is DerivedClass a derived class of RootClass? {0}",
derived.GetType ().IsSubclassOf (root.GetType() )); //True
Console.Read();
}
}
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.WriteLi ne("Is Array a derived class of int[]? {0}",
typeof(Array).I sSubclassOf(int Array.GetType() )); //False
Console.WriteLi ne("Is int [] a derived class of Array? {0}",
intArray.GetTyp e().IsSubclassO f(typeof(Array) )); //True
Console.WriteLi ne("Is IRoot a derived class of IDerived? {0}",
typeof(IRoot).I sSubclassOf(typ eof(IDerived))) ; //False
Console.WriteLi ne("Is IDerived a derived class of IRoot? {0}",
typeof(IDerived ).IsSubclassOf( typeof(IRoot))) ; //False???
Console.WriteLi ne("Is RootClass a derived class of DerivedClass? {0}",
root.GetType(). IsSubclassOf(de rived.GetType() )); //False
Console.WriteLi ne("Is DerivedClass a derived class of RootClass? {0}",
derived.GetType ().IsSubclassOf (root.GetType() )); //True
Console.Read();
}
}
Comment