473,503 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using GetType on a Generic Type

Hi All,

How can I use GetType(<GenericType>).IsAssignableFrom(<MyType>)

I need to now if the <MyType> is the same type of class as the <GenericType>
without having to add the generic type member members.

i.e. I do not want to use GetType(<GenericType>(Of
String).IsAssignableFrom(<MyType>)

I've tried using
GetType(<GenericType>(Of ,).IsAssignableFrom(<MyType>) but it always returns
false.

Regards
Neal
Dec 12 '05 #1
3 4452
"Joe Adams" <a@b.com> schrieb:
How can I use GetType(<GenericType>).IsAssignableFrom(<MyType>)

I need to now if the <MyType> is the same type of class as the
<GenericType> without having to add the generic type member members.

i.e. I do not want to use GetType(<GenericType>(Of
String).IsAssignableFrom(<MyType>)

I've tried using
GetType(<GenericType>(Of ,).IsAssignableFrom(<MyType>) but it always
returns false.


Assuming you have two types 'A' and 'B', and 'B' inherits from 'A'. In
addition, you have a generic class 'Foo(Of T)'. In this case, 'Foo(Of A)'
is not the supertype of 'Foo(Of B)', althoigh 'A' is the supertype of 'B'!

Thus
'GetType(Object).IsAssignableFrom(Foo(Of ))'
will evaluate to 'True' while
'GetType(Foo(Of A)).IsAssignableFrom(GetType(Foo(Of B))'
evaluates to 'False'.

Assuming you have these classes

\\\
Public Class Foo(Of T)
End Class

Public Class Goo
Inherits Goo(Of Integer)
End Class

Public Class Boo
Inherits Goo
End Class
///

then
'GetType(Foo(Of ).IsAssignableFrom(GetType(Goo))'
will return 'False', although
'GetType(Goo).IsAssignableFrom(GetType(Boo))'
returns 'True'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Dec 12 '05 #2
Thank's for that, So it appears that IsAssignable function only works with
specified generics types not generic definition types.

The following function now does what I need.
Called like so

IsAssignableFromGenericType(<SomeTypeToCheck>,
GetType(BusinessListBase(Of )))
Private Function IsAssignableFromGenericType(ByVal typeToCheck As
System.Type, ByVal genericTypeToCompare As System.Type) As Boolean

Do

If typeToCheck.IsGenericType Then

Dim genTypeDef As System.Type = typeToCheck.GetGenericTypeDefinition

If genTypeDef.Equals(genericTypeToCompare) Then

Return True

End If

End If

typeToCheck = typeToCheck.BaseType

Loop While typeToCheck.BaseType IsNot Nothing

End Function
Regards

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:u$**************@TK2MSFTNGP14.phx.gbl...
"Joe Adams" <a@b.com> schrieb:
How can I use GetType(<GenericType>).IsAssignableFrom(<MyType>)

I need to now if the <MyType> is the same type of class as the
<GenericType> without having to add the generic type member members.

i.e. I do not want to use GetType(<GenericType>(Of
String).IsAssignableFrom(<MyType>)

I've tried using
GetType(<GenericType>(Of ,).IsAssignableFrom(<MyType>) but it always
returns false.


Assuming you have two types 'A' and 'B', and 'B' inherits from 'A'. In
addition, you have a generic class 'Foo(Of T)'. In this case, 'Foo(Of A)'
is not the supertype of 'Foo(Of B)', althoigh 'A' is the supertype of 'B'!

Thus
'GetType(Object).IsAssignableFrom(Foo(Of ))'
will evaluate to 'True' while
'GetType(Foo(Of A)).IsAssignableFrom(GetType(Foo(Of B))'
evaluates to 'False'.

Assuming you have these classes

\\\
Public Class Foo(Of T)
End Class

Public Class Goo
Inherits Goo(Of Integer)
End Class

Public Class Boo
Inherits Goo
End Class
///

then
'GetType(Foo(Of ).IsAssignableFrom(GetType(Goo))'
will return 'False', although
'GetType(Goo).IsAssignableFrom(GetType(Boo))'
returns 'True'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Dec 13 '05 #3
"Joe Adams" <a@b.com> schrieb:
Thank's for that, So it appears that IsAssignable function only works with
specified generics types not generic definition types.


Yes, that's true. Otherwise there is no subtyping relation between the
types.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Dec 13 '05 #4

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

Similar topics

4
1374
by: David Douglass | last post by:
I'm confused about the program below. Based on my reading of the C# spec, I don't think it should compile, but it does when using Beta 1. Could somebody please explain the function selection...
5
4558
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public...
4
1548
by: Gazarsgo | last post by:
This seems to be a bit of a contradiction, but how can I construct a Generic class using a System.Type variable that is assigned at run time? Is there some parallel to the Generics concept that...
16
4737
by: danielbuus | last post by:
....or, to put it perhaps more communicative, something like this: Type someObjectsType = someObject.GetType(); someObjectsType newObject = new someObjectsType(); Is this possible? If so, how?...
0
5918
by: crazyone | last post by:
I've got a gaming framework i'm building and i want to save myself the trouble of reading and writting the complete game data to a custom file and load/save it to an XML file but i'm getting...
1
2826
by: learning | last post by:
Hi how can I instaltiate a class and call its method. the class has non default constructor. all examples i see only with class of defatul constructor. I am trying to pull the unit test out from...
4
2008
by: =?Utf-8?B?SGF5U2VlZA==?= | last post by:
Is there some way to use Generics in dynamic code using the Type.GetType("MyClassName") as an argument? List<Type.GetType("MyClassName") oList = new List<Type.GetType("MyClassName") > ...
1
1681
by: Jon Slaughter | last post by:
Type asdf = Type.GetType(t.GetType().FullName); How come something like that returns a null type when t is a generic type? It works fine for non-generic types.
2
3032
by: jon | last post by:
I'm trying to write a block of code that can create an instance of a generic object, with the Type coming in as a dynamic string. It isn't working yet. Any advice how I can dynamically create the...
0
7202
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,...
0
7278
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,...
0
7328
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...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7458
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
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,...
1
5013
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...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
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 ...

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.