Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 170517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:49:20+00:00 2026-05-11T12:49:20+00:00

Given an extension method like this: Public Sub RehydrateTo(Of T As New)(ByVal input As

  • 0

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?

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T12:49:21+00:00Added an answer on May 11, 2026 at 12:49 pm

    The strange behavior of T is only in the exception handler, so you can just move the code out of the handler, like this:

    Dim exceptionCaught As Boolean Try    output = CType(f.ReadObject(ms), T) Catch ex As SerializationException    exceptionCaught = True End Try  If Not exceptionCaught Then    Exit Sub End If  'here we put the code that we want to handle the exception 

    The problem is that the exception handler is ‘code shared’ across multiple types. when we’re in the exception handler, we aren’t in code specific to any class we wrote, we’re in the canonical instantiation of a generic — this is why T = System.__Canon.

    The workaround is just to not evaluate T while in a code-shared block.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an HtmlHelper extension method that looks like this: public static MvcHtmlString TextBoxWithMaxLengthFor<TModel,
I am writing an extension method for parsing JSON string for any given type.
Let's say we've got a generic list like this: List<string> list = new List<string>()
1.First I defined an extension method for the IEnumerable.Add() like the code below public
Given this method to work on a HTML page in a webbrowser: bool semaphoreForDocCompletedEvent;
Given paths like this: C:\Temp\SomeDirectory\*.xml I'd like to be able to distinguish the *.xml
I have a method and I want to add this method as an extension
This is what my Html helper looks like: namespace WebApp.WebUI { public static class
Lets say I have this extention method: public static bool HasFive<T>(this IEnumerable<T> subjects) {
I'd like to create a data table given a List using the CopyToDataTable method

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.