473,406 Members | 2,707 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,406 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 6517
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: 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?
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.