468,527 Members | 2,109 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,527 developers. It's quick & easy.

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 6088
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Kristofer Pettijohn | last post: by
2 posts views Thread by Trevor Andrew | last post: by
23 posts views Thread by René Nordby | last post: by
7 posts views Thread by =?Utf-8?B?Sm9lbCBNZXJr?= | last post: by
11 posts views Thread by Fuzzyman | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.