PC Review


Reply
Thread Tools Rate Thread

Type.IsSubclassOf

 
 
ME
Guest
Posts: n/a
 
      22nd May 2005
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();
}
}


 
Reply With Quote
 
 
 
 
Sean Hederman
Guest
Posts: n/a
 
      22nd May 2005
"ME" <(E-Mail Removed)> wrote in message
news:0I2dnZocj5K7Kw3fRVn-(E-Mail Removed)...
> 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();
> }
> }
>



 
Reply With Quote
 
ME
Guest
Posts: n/a
 
      22nd May 2005
Thanks!

Matt
"Sean Hederman" <(E-Mail Removed)> wrote in message
news:d6qg8i$l16$(E-Mail Removed)...
> "ME" <(E-Mail Removed)> wrote in message
> news:0I2dnZocj5K7Kw3fRVn-(E-Mail Removed)...
>> 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();
>> }
>> }
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with IsSubclassOf Paguro Bernardo Microsoft C# .NET 9 19th Jan 2006 11:29 AM
IsSubclassOf Fails!!! Mark Olbert Microsoft Dot NET Framework 10 16th Dec 2005 07:58 AM
Type.IsSubclassOf ME Microsoft Dot NET 2 22nd May 2005 10:01 PM
Reflection - IsSubclassOf thomas Microsoft C# .NET 3 12th May 2004 10:03 AM
Would IsSubclassOf() work here? Daniel Billingsley Microsoft Dot NET Framework 2 7th May 2004 08:47 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:13 PM.