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();
}
} 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(); } }
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(); } }
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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;}
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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
// \
// ...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |