Type.IsSubclassOf

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ME

    #1

    Type.IsSubclassOf

    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();
    }
    }


  • Sean Hederman

    #2
    Re: Type.IsSubclass Of

    "ME" <trash.trash@co mcast.netREMOVE THIS> wrote in message
    news:0I2dnZocj5 K7Kw3fRVn-hw@comcast.com. ..[color=blue]
    > 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.[/color]

    Use typeof(IRoot).I sAssignableFrom (typeof(IDerive d)), 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.
    [color=blue]
    >
    > 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();
    > }
    > }
    >[/color]


    Comment

    • ME

      #3
      Re: Type.IsSubclass Of

      Thanks!

      Matt
      "Sean Hederman" <email.jpg@codi ngsanity.blogsp ot.com> wrote in message
      news:d6qg8i$l16 $1@ctb-nnrp2.saix.net. ..[color=blue]
      > "ME" <trash.trash@co mcast.netREMOVE THIS> wrote in message
      > news:0I2dnZocj5 K7Kw3fRVn-hw@comcast.com. ..[color=green]
      >> 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.[/color]
      >
      > Use typeof(IRoot).I sAssignableFrom (typeof(IDerive d)), 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.
      >[color=green]
      >>
      >> 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();
      >> }
      >> }
      >>[/color]
      >
      >[/color]


      Comment

      Working...