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 3523
"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: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |