473,387 Members | 1,890 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,387 software developers and data experts.

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

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.GetGe nericTypeDefinition
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

Aug 21 '06 #1
2 9844
>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/VisualS...edbackID=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.
Aug 21 '06 #2

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

Aug 22 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
1
by: Dave | last post by:
Hello NG, Regarding access-declarations and member using-declarations as used to change the access level of an inherited base member... Two things need to be considered when determining an...
1
by: Mark McDonald | last post by:
This question kind of follows on from Mike Spass’ posting 10/11/2004; I don’t understand why you can’t declare an implicit operator to convert a base class to a derived class. The text...
6
by: roland.bali | last post by:
Hi, Here is the basic setup, my base class is Shoe which has a child class called Sandal. I would like to create objects by calling Sandal.Load. But without overloading Load in Sandal and...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
47
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever...
2
by: kasthurirangan.balaji | last post by:
Hello, template<class Base> class Derived : public Base { }; By using template, i understand the actual base type will be deduced at compile time. Moreover, class Derived will consist only...
3
by: Ravi | last post by:
Is this the correct way to think of "base class"? The "base class" is a class from which other classes are derived. The "base class" will never be derived from another class.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.