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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:58:01+00:00 2026-06-02T00:58:01+00:00

According to these release notes , Json.NET now supports the SerializableAttribute: Json.NET now detects

  • 0

According to these release notes, Json.NET now supports the SerializableAttribute:

Json.NET now detects types that have the SerializableAttribute and serializes all the fields on that type, both public and private, and ignores the properties.

I have the following sample code that throws a JsonSerializationException:

Error getting value from ‘CS$<>9__CachedAnonymousMethodDelegate1’ on
‘ConsoleApplication1.MyType’.

If I comment the TotalWithLambda property, then the serialization succeeds as expected. In fact, I get the following results:

  • Leave [Serializable], leave TotalWithLambda: throws JsonSerializationException
  • Leave [Serializable], remove TotalWithLambda: serializes “myList” only
  • Remove [Serializable], leave TotalWithLambda: serializes “myList”, “Total”, and “TotalWithLambda”
  • Remove [Serializable], remove TotalWithLambda: serializes “myList” and “Total”

I understand all of these cases except the first one. Why does the combination of [Serializable] and a read-only property with a lambda in it cause this exception?

namespace ConsoleApplication1
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Newtonsoft.Json;

    class Program
    {
        static void Main(string[] args)
        {
            var foo = new MyType();
            foo.myList = new List<int>() { 0, 1, 2, 3 };

            var returnVal = JsonConvert.SerializeObject(foo);

            Console.WriteLine("Return: " + returnVal.ToString());
            Console.ReadKey();
        }
    }

    [Serializable]
    class MyType
    {
        public IList<int> myList;
        public int Total { get { return this.myList.Sum(); } }
        public int TotalWithLambda { get { return this.myList.Sum(x => x); } }
    }

}
  • 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-06-02T00:58:03+00:00Added an answer on June 2, 2026 at 12:58 am

    I installed and used JustDecompile and found that the compiler adds a field and a method to the class when the lambda is uncommented:

    public class MyType
    {
        [CompilerGenerated]
        private static Func<int, int> CS$<>9__CachedAnonymousMethodDelegate1;
    
        [CompilerGenerated]
        private static int <get_TotalWithLambda>b__0(int x) { ... }
    
        // ... plus the other class members ...
    }
    

    When the class has SerializableAttribute on it, Json.NET tries to serialize the private field, but can’t since it’s of type Func<int, int>. Removing the SerializableAttribute instructs Json.NET to ignore private fields and so it doesn’t cause a problem.

    Update: Json.NET 4.5 release 3 now makes this only a problem if you explicitly set IgnoreSerializableAttribute=false, or it can be resolved by adding the JsonObjectAttribute to the class..

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

Sidebar

Related Questions

According to the Ruby on Rails 2.3 Release Notes... if your plugin has an
Right now I have an application that loads a bunch of thumbnail images into
I have a question about the Maven Release plugin. According to the plugin documentation,
I have access to the HL7 Clinical Document Architecture, Release 2.0, which states that
I have an iOS (Obj-C) project that uses ARC (Automatic Reference Counting). According to
According to various soures, for a Qt4 app you build the release or debug
I have been working on an iPhone project, where we created all the user
Okay, what I currently have is a TabBarController with five tabs. Each of these
I'm confused by an occasional crash that I'm seeing, which, according to the Zombies
I'll start by saying that over the course of a few projects, I have

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.