473,388 Members | 1,146 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,388 software developers and data experts.

Determining if one type inherits from another type

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 direction?

Thanks in advance,
Damien
Aug 21 '08 #1
7 6516
For classes, you can use:

type1.IsSubclassOf(type2)

If you mean interfaces, this i slightly tricker - but IsAssignableFrom
might do the job.

Marc
Aug 21 '08 #2
On Aug 21, 12:53*pm, Marc Gravell <marc.grav...@gmail.comwrote:
For classes, you can use:

type1.IsSubclassOf(type2)

If you mean interfaces, this i slightly tricker - but IsAssignableFrom
might do the job.
Note that IsSubclassOf will return false if type1==type2 (which is
expected, given its name, but may be surprising in a sense that this
behavior is different from the "is" operator).

The most generic way to do this, which will handle the case described
above, as well as interfaces, is indeed Type.IsAssignableFrom.
Aug 21 '08 #3
The most generic way to do this, which will handle the case described
above, as well as interfaces, is indeed Type.IsAssignableFrom.
Granted; of course, it depends a bit on what is *actually* intended by
"if one type is inheritted from another type", and what the OP wants
that to return for "Foo op Foo".

Marc
Aug 21 '08 #4
On Aug 21, 7:45*am, Pavel Minaev <int...@gmail.comwrote:
On Aug 21, 12:53*pm, Marc Gravell <marc.grav...@gmail.comwrote:
For classes, you can use:
type1.IsSubclassOf(type2)
If you mean interfaces, this i slightly tricker - but IsAssignableFrom
might do the job.

Note that IsSubclassOf will return false if type1==type2 (which is
expected, given its name, but may be surprising in a sense that this
behavior is different from the "is" operator).

The most generic way to do this, which will handle the case described
above, as well as interfaces, is indeed Type.IsAssignableFrom.
To piggyback on this thread and learn something - can't the same be
accomplished by using "is"?
Aug 21 '08 #5
"is" will only work for an instance (left-hand operand) and a type
(right-hand operand), so it depends on the full scenario. In this case
it sounds like there is no instance, and the type is known only at
runtime, so "is" can't be used. Of course, my interpretation of the
problem might be incorrect...

Marc
Aug 21 '08 #6
On Aug 21, 5:49*pm, "G.S." <gstoy...@gmail.comwrote:
To piggyback on this thread and learn something - can't the same be
accomplished by using *"is"?
It can, if you have a specific value, and know the type you want to
check for in advance. What we were discussing is rather this
situation:

void Foo(Type type1, Type type2) {
// Need to determine of type1 inherits from type2
}

Aug 22 '08 #7
Thanks for that guys. Very helpful.

For the record...
In this case
it sounds like there is no instance, and the type is known only at
runtime, so "is" can't be used.
.... that was correct in this case.

Damien

Aug 25 '08 #8

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

Similar topics

1
by: Kristofer Pettijohn | last post by:
Greetings, I'm trying to figure out if there is a way to determine whether a class has the traits of another class... For example... I have a class called Square, which inherits Cube, which...
7
by: CJM | last post by:
I'm finally taking my first few steps with ASP.NET, and I'm just workign my way through some of the basics. As a vehicle for my learning, I'm re-writing a simple report page for on of my ASP...
2
by: Trevor Andrew | last post by:
Hi There, I have a usercontrol that presents the majority of a page's content. Within the ASP.NET application it appears as XXXX.PageControl, where XXXX is the namespace of my project. There...
23
by: René Nordby | last post by:
Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than...
4
by: petermichaux | last post by:
Hi, I'm hoping for a reason I'm wrong or an alternate solution... I'd like to be able to dynamically include some javascript files. This is like scriptaculous.js library but their solution is...
7
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds...
11
by: Fuzzyman | last post by:
Hello all, I may well be being dumb (it has happened before), but I'm struggling to fix some code breakage with Python 2.6. I have some code that looks for the '__lt__' method on a class: ...
8
by: Fuzzyman | last post by:
Hello all, I may well be being dumb (it has happened before), but I'm struggling to fix some code breakage with Python 2.6. I have some code that looks for the '__lt__' method on a class: ...
0
by: Fuzzyman | last post by:
Hello all, Sorry - my messages aren't showing up via google groups, so I'm kind of posting on faith... Anyway, I solved my problem (I think)... import sys if sys.version_info == 3:
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.