473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Type.IsSubclassOf

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();
}
}
Nov 22 '05 #1
2 3530
"ME" <tr*********@comcast.netREMOVETHIS> wrote in message
news:0I********************@comcast.com...
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();
}
}

Nov 22 '05 #2
ME
Thanks!

Matt
"Sean Hederman" <em*******@codingsanity.blogspot.com> wrote in message
news:d6**********@ctb-nnrp2.saix.net...
"ME" <tr*********@comcast.netREMOVETHIS> wrote in message
news:0I********************@comcast.com...
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();
}
}


Nov 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ME | last post by:
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...
6
by: MP | last post by:
Hello I want to have a public property (and Brows able) in one control, I use this code: public System.Windows.Forms.Form recordForm get { return _recordForm;} set {_recordForm = value;}
2
by: Abdessamad Belangour | last post by:
Hi all, within the same assembly, we are capable to retrieve the superTypes of a given Type object by using the IsSubclassOf method of the Type class. What about classes that belongs to external...
1
by: Zorpiedoman | last post by:
I'm pretty new (ok, REALLY new) to reflection stuff. It is really coming in handy right now in a project I'm working on. I've figured out how to disect a .dll file and get all the modules, then...
13
by: Fredrik Strandberg | last post by:
Hi! I receive an object as a System.Object argument to a method. I need to check if the object is a Type object or not (that is, not a specific type, but if the object is a type object in...
10
by: marc | last post by:
Hoi there, I am a Delphi Programer who moved over to C# some months ago. So far I am really happy with C# and feelt myself confortable quite fast. Just some advanced questions regarding the type...
2
by: AdawayNoSpam | last post by:
Said that I have the following class Class MyRootClass(Of T) End Class Class MySubClass1(Of T) Inherits MyRootClass(Of T) End Class
8
by: bonk | last post by:
When I have an instance of an object wich is of type System.Type, how can I find out if it directly or indirecly derives from another type? myTypeInstance == typeof(AnotherType) only seems to...
5
by: per9000 | last post by:
Hi all, I have a question I am unable solve. I have an inheritance graph like this: // CAR ----- MUSICAR ---- NICECAR // \ \ // \ +------ OLDCAR // \ // ...
7
by: DamienS | last post by:
Hi, I need to determine if one type is inheritted from another type. I don't have instances of the objects (and it's undesirable to create them). Can someone please point me in the right...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.