473,769 Members | 6,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6536
For classes, you can use:

type1.IsSubclas sOf(type2)

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

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

type1.IsSubclas sOf(type2)

If you mean interfaces, this i slightly tricker - but IsAssignableFro m
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.IsAssignab leFrom.
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.IsAssignab leFrom.
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.c omwrote:
On Aug 21, 12:53*pm, Marc Gravell <marc.grav...@g mail.comwrote:
For classes, you can use:
type1.IsSubclas sOf(type2)
If you mean interfaces, this i slightly tricker - but IsAssignableFro m
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.IsAssignab leFrom.
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
1523
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 inherits Shape .. I am writing another class that will get passed a class as a template
7
2329
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 applications. I have two tables on this page. One is where the user requests a report for a given ID number, the second is the table of result. Obviously I dont want to show the results table until the query has been submitted, so I'm trying to...
2
1868
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 will only ever be one and onely one of these controls per page. I want to identify the control and set a particular property depending upon what the current page is. I want to find this control generically, so I wrote the following code:
23
3523
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 the Inherit statement).
4
2161
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 broken in Firefox 1.5.0.1 on OS X. What happens with the Scriptaculous library is this In the html document the author only has to include one line
7
2845
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 to support cloning: Public Class RefClass Public tcp As TcpClient Public name As String End Class Public Class RefClassList Inherits List(Of RefClass) Implements ICloneable
11
1663
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: if hasattr(clr, '__lt__'): However - in Python 2.6 object has grown a default implementation of
8
2616
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: if hasattr(clr, '__lt__'): However - in Python 2.6 object has grown a default implementation of
0
903
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
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10045
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8870
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7408
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.