![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I hope someone out there can help me with this, because I am stuck. My problem is that I have an instance of a generic class, but I don't know ahead of time what the type parameter used was. In my case the type must derive from a class, like UserControl, so I at least know I should be able to use that. I can not figure out a way to get this as an instance of the generic class, but with the base class as the type parameter. |
#3
| |||
| |||
|
|
I hope someone out there can help me with this, because I am stuck. My problem is that I have an instance of a generic class, but I don't know ahead of time what the type parameter used was. In my case the type must derive from a class, like UserControl, so I at least know I should be able to use that. I can not figure out a way to get this as an instance of the generic class, but with the base class as the type parameter. You can't - generics don't support covariance/contravariance. So, for example, a List<string> isn't compatible with List<object>. In particular, imagine what would happen if you tried to add a new plain object to it... See Eric Lippert's blog for more information on this: http://blogs.msdn.com/ericlippert/ar...e+and+Contrava r iance/default.aspx |
#4
| |||
| |||
|
|
Actually, I do read his blog, one of the few from MS. It usually hurts my brain, and the sense it makes is only flitting. |

|
So basically, I have this _thing_, and there is nothing anywhere that I can convert it to in order to call methods on it. I can't convert it to the base class, and call base class methods. I recognize the similarity, but something in me won't see why it is the same case. Is it not allowed because, somehow at some point I could write to that object (which I used the generic for) with a type that would be contravaint?, ie, I could theoretically try to write a turtle into my giraffe, and the compiler can't tell that I am not going to do that, so it just doesn't allow the conversion at all? |
|
I read I can do it by poking at Ms.Vb.CompilerServices.Conversions, but I don't think thats a good idea. |
#5
| |||
| |||
|
|
It may not help, but it just might do... I read I can do it by poking at Ms.Vb.CompilerServices.Conversions, but I don't think thats a good idea. Hmm... it sounds unlikely to me, to be honest. The CLR understands covariance/contravariance on interfaces, but that's all as far as I'm aware. Of course, VB could do it all with late binding, but that doesn't sound like a good idea either. |
#6
| |||
| |||
|
|
I hope someone out there can help me with this, because I am stuck. My problem is that I have an instance of a generic class, but I don't know ahead of time what the type parameter used was. In my case the type must derive from a class, like UserControl, so I at least know I should be able to use that. I can not figure out a way to get this as an instance of the generic class, but with the base class as the type parameter. I am looking to do something like this : ..Dim formRef as Form = /get-reference-to-mycontainer-instance/ ..If ( TypeOf formRef Is MyContainer(Of UserControl) ) Then ...... Dim a As MyContainer(Of UserControl) = CType(formRef, MyContainer(Of UserControl)) ...... do stuff ..End If Here is the generic class: ------- ..Public Class MyContainer(Of T As {UserControl, New}) ......Inherits System.Windows.Forms.Form ...... ..End Class Is there any way to do something like this? Even something ugly? Thanks, //Andrew |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |