How to check base class type of 'open' generic class? (inheritance check)

G

Guest

Said that I have the following class

Class MyRootClass(Of T)
End Class

Class MySubClass1(Of T)
Inherits MyRootClass(Of T)
End Class


Hwo can I check, at runtime, if the type of the object 'Obj' (of type
MySubClass1), is a subclass of MyRootClass?

I don't want to care about the type used by the instance/declaration of
the generic class, they can be string, integer, other object.... this
is not important for my check
I need to check the type of the generic class, independently from the
type used by the various declaration/instance of my generic class.

Dim r As New MyRootClass(Of anything1)
Dim s1 As New MySubClass1(Of anything2)

'the following code return true, this is ok
s1.GetType.GetGenericTypeDefinition.BaseType.GetGenericTypeDefinition
Is r.GetType.GetGenericTypeDefinition

'but, why the following code return FALSE?
s1.GetType.GetGenericTypeDefinition.IsSubclassOf(r.GetType.GetGenericTypeDefinition)


Why I'm unable to check inheritance of open generic type?

Thanks, adaway
 
M

Mattias Sjögren

I don't want to care about the type used by the instance/declaration of
the generic class, they can be string, integer, other object.... this
is not important for my check

So how will you act on the result of the check? If you don't care
about the type argument, you probably have some functionality in the
base class that is independent of T. If so, why don't you move that to
a non-generic base class or interface and check for that instead?

'but, why the following code return FALSE?
s1.GetType.GetGenericTypeDefinition.IsSubclassOf(r.GetType.GetGenericTypeDefinition)


Why I'm unable to check inheritance of open generic type?

It seems a bit weird, doesn't it? But apparently it's by design.

http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=95768


Mattias
 
G

Guest

Mattias said:
So how will you act on the result of the check? If you don't care
about the type argument, you probably have some functionality in the
base class that is independent of T. If so, why don't you move that to
a non-generic base class or interface and check for that instead?

I get a collection of object from
BindingSource.GetItemProperties(nothing)
This collection contain 2 tipe of object, the first represent .Net type
(string, int32...)
the represent a generic class.
I can have different generic class of type A/B/C
A ad B inherits from MidClass (generics)
MidClass inherits from BaseClass (generics)
C inherits from BaseClass (generics)

Now, I need to create a new collection, changing the order of the item
that are of type A/B/C, so I need to check if they are subclass of
BaseClass (generics).

It seems a bit weird, doesn't it? But apparently it's by design.

Maybe it's by desing, but I think it's inconsistent.

if SubClass.BaseType Is RootType is true,
why SubClass.IsSubClassOf(RootType) is false?

This seem inconsistent to me.

Adaway
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top