Given an extension method like this:
Public Sub RehydrateTo(Of T As New)(ByVal input As String, ByRef output As T)
Dim ms As MemoryStream = MsFromString(input)
Dim f As New DataContractSerializer(GetType(T))
Try
output = CType(f.ReadObject(ms), T)
Catch ex As SerializationException
output = New T
Dim ild As ILegacyDeserializer = TryCast(output, ILegacyDeserializer)
If Not ild Is Nothing Then
' ... you get the idea
End If
End Try
End Sub
and a type MyCollection that inherits from ObservableCollection(Of V), we find that calling someString.RehydrateTo(instanceOfMyCollection) can fail in the exception handler. The problem is that GetType(T) does not always evaluate to "MyCollection" -- while in the exception handler, it evaluates to "__Canon".
( System.__Canon being some kind of CLR magic that means a canonical instantiation of a generic )
How can we work around this?