473,809 Members | 2,742 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(<Generi cType>).IsAssig nableFrom(<MyTy pe>)

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(<Generi cType>(Of
String).IsAssig nableFrom(<MyTy pe>)

I've tried using
GetType(<Generi cType>(Of ,).IsAssignable From(<MyType>) but it always returns
false.

Regards
Neal
Dec 12 '05 #1
3 4468
"Joe Adams" <a@b.com> schrieb:
How can I use GetType(<Generi cType>).IsAssig nableFrom(<MyTy pe>)

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(<Generi cType>(Of
String).IsAssig nableFrom(<MyTy pe>)

I've tried using
GetType(<Generi cType>(Of ,).IsAssignable From(<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 ).IsAssignableF rom(Foo(Of ))'
will evaluate to 'True' while
'GetType(Foo(Of A)).IsAssignabl eFrom(GetType(F oo(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 ).IsAssignableF rom(GetType(Goo ))'
will return 'False', although
'GetType(Goo).I sAssignableFrom (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

IsAssignableFro mGenericType(<S omeTypeToCheck> ,
GetType(Busines sListBase(Of )))
Private Function IsAssignableFro mGenericType(By Val typeToCheck As
System.Type, ByVal genericTypeToCo mpare As System.Type) As Boolean

Do

If typeToCheck.IsG enericType Then

Dim genTypeDef As System.Type = typeToCheck.Get GenericTypeDefi nition

If genTypeDef.Equa ls(genericTypeT oCompare) Then

Return True

End If

End If

typeToCheck = typeToCheck.Bas eType

Loop While typeToCheck.Bas eType IsNot Nothing

End Function
Regards

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:u$******** ******@TK2MSFTN GP14.phx.gbl...
"Joe Adams" <a@b.com> schrieb:
How can I use GetType(<Generi cType>).IsAssig nableFrom(<MyTy pe>)

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(<Generi cType>(Of
String).IsAssig nableFrom(<MyTy pe>)

I've tried using
GetType(<Generi cType>(Of ,).IsAssignable From(<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 ).IsAssignableF rom(Foo(Of ))'
will evaluate to 'True' while
'GetType(Foo(Of A)).IsAssignabl eFrom(GetType(F oo(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 ).IsAssignableF rom(GetType(Goo ))'
will return 'False', although
'GetType(Goo).I sAssignableFrom (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
1395
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 rules to me? Thanks, -- David Douglass MCSD for Microsoft .NET =================================================== #region Using directives
5
4583
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 constructors (because we do not allow developers to instantiate them directly). Because our business objects are instantiated very frequently, the idea of using reflection sounds like a performance killer (I haven't done any tests on this, but the...
4
1563
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 extends to having strictly-typed classes at run-time? I would gladly give up the compile-time errors if I could get rid of all these CType()s :)
16
4766
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? Thanks very much in advance, Daniel :)
0
5931
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 problem serializing my stuff to XML when it comes to collections. I'm currently using .net2 with generic lists to prevent users putting all sorts of stuff in the arrays (Although im sure i'll be the only user of the classes but not the game, anyway)....
1
2838
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 the product source code, but still want to execute them under nunit. I am trying this idea on nunit sample source code. Here is my class and the experiemental code: both money.cs and Imoney.cs is compiled to cs_money.dll. then I create another...
4
2022
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") > .... or is there a way to declare a generic using strings that hold class
1
1712
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
3051
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 appropriate object to pass into the generic's constructing <T,U>? ------------------------------- // fully qualified types as string string type1Str = "ns1.ns2.control1, software.lib1"; string type2Str = "ns1.ns2.control2, software.lib1";
0
9721
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9601
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10379
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10115
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
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 we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.