Home All Groups Group Topic Archive Search About

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

Author
21 Aug 2006 1:02 PM
AdawayNoSpam
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

Author
21 Aug 2006 9:11 PM
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

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
22 Aug 2006 7:36 AM
AdawayNoSpam
Mattias Sjögren wrote:
> >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?

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).


> >'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.

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