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 1012305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:55:32+00:00 2026-05-16T09:55:32+00:00

Given the following declaration: <Extension()> Public Function ToJSON(ByVal target As Object) As String Dim

  • 0

Given the following declaration:

<Extension()> Public Function ToJSON(ByVal target As Object) As String
    Dim serializer = New System.Runtime.Serialization.Json.DataContractJsonSerializer(target.GetType)
    Using ms As MemoryStream = New MemoryStream()
        serializer.WriteObject(ms, target)
        ms.Flush()

        Dim bytes As Byte() = ms.ToArray() 

        Dim json As String = Encoding.UTF8.GetString(bytes, 0, bytes.Length)

        Return json
    End Using
End Function

And the following lines in the Page_Load of a test page:

Dim kvp = New System.Collections.Generic.KeyValuePair(Of String, Object)(
    "date", New HttpCookie("woot", "yikes")
)
Put(New HttpCookie("woot", "yikes").ToJSON)
Put(kvp.ToJSON)
Put(kvp.Value.ToJSON)
Put("here".ToJSON())

The first Put works perfect, and puts out the following JSON:

{"Domain":null,"Expires":"\/Date(-62135578800000-0500)\/",
 "HttpOnly":false,"Name":"woot","Path":"\/",
 "Secure":false,"Value":"yikes"}

The second Put, however, throws a giant, ugly error as so:

Type ‘System.Web.HttpCookie’ with data contract name ‘HttpCookie:http://schemas.datacontract.org/2004/07/System.Web‘ is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types – for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

The third Put throws an error also, but COMPLETELY different:

Public member ‘ToJSON’ on type ‘HttpCookie’ not found.

And the fourth Put works perfect.

I am very confused why, when the first line works, and the Extension method is clearly being found on the HttpCookie object, why then in the 2nd and 3rd Puts does it NOT work, and why do I get a different error in both cases? Each of the first three Puts is trying to do the same thing – call the ToJSON extension method on the HttpCookie object.

All exposition welcome!

  • 1 1 Answer
  • 0 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. Editorial Team
    Editorial Team
    2026-05-16T09:55:32+00:00Added an answer on May 16, 2026 at 9:55 am

    The problem with the third Put is that VB doesn’t support extension methods on anything declared to be of type Object: VB.NET: impossible to use Extension method on System.Object instance

    This will work: Put(ToJSON(kvp.Value))

    And so will this:

    Dim kvp = New System.Collections.Generic.KeyValuePair(Of String, HttpCookie)( 
        "date", New HttpCookie("woot", "yikes")) 
    Put(kvp.Value.ToJSON) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following... public MyClass { public void MyClass() { var myWorkerClass = new
Given the following Delphil DLL declaration function csd_HandleData(aBuf: PChar; aLen: integer): integer; stdcall; what
In Matlab, how can i get a String containing GRUMPY given the following declaration:
Given following Statment: String query = "Select * from T_spareParts where SparePartPK IN (?
Given the following code: $(.force-selection).blur(function() { var value = $('matched-item').val(); //check if the input's
Given the following code: function Person(firstName, lastName) { this.FirstName = firstName; this.LastName = lastName;
Given the following declaration: double x; What is the value of x ? Is
I have been given a header with the following declaration: //The index of 1
Given the following C++ code which doesn't mention a calling convention in the function
Given the following C# code to generate an XML file: XmlDocument requestXML = new

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.