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

Ask A Question

Stats

  • Questions 533k
  • Answers 533k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'm assuming you are transmitting your data with JSON/XML. Try… May 17, 2026 at 12:33 am
  • Editorial Team
    Editorial Team added an answer if ( v > 145 ) size = 1; else… May 17, 2026 at 12:33 am
  • Editorial Team
    Editorial Team added an answer Doctrine_Manager::connection('mysql://user:pass@host/schema') // This must be the charset of your HTTP… May 17, 2026 at 12:33 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Given the following Delphil DLL declaration function csd_HandleData(aBuf: PChar; aLen: integer): integer; stdcall; what
I have been given a header with the following declaration: //The index of 1
Given the following C# code to generate an XML file: XmlDocument requestXML = new
Given the following code class T { public: virtual ~T () {} virtual void
Given the following InsertItemTemplate (simplified) I'm not getting anything back in the event object's
Given the following: module MyModule = let myObj = new MyObj() type MyType() =
Given the following Java which is loaded into the database using loadjava: package grassie.example;
Given the following code snippet from inside a method; NSBezierPath * tempPath = [NSBezierPath
Given the following code (it's a macro that generates code for a list data
Given the following input file: a = 2 b = 3 c = a

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.