This project has moved. For the latest updates, please go here.


InvalidProgramException when serializing complex object


I have a complex view model structure. When I try to send this view model to as a ApiController return value and let WebApi use the JsonMediaTypeFormatter, I get an InvalidProgramException: "Error getting value from 'CS$<>9__CachedAnonymousMethodDelegate2' on 'blabla.ViewModels.AnswerFilterModel'."

Exception callstack:
   bei Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)
   bei Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue)
   bei Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   bei Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
   bei Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value)
   bei Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value)
   bei Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value)
   bei System.Net.Http.Formatting.JsonMediaTypeFormatter.<>c__DisplayClassd.b__c()
   bei System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action, CancellationToken token)
The model that causes this exception looks like this:
public class AnswerFilterModel
    private readonly List answerValues;

    /// Initializes a new instance of the  class.
    public AnswerFilterModel()
        this.answerValues = (from answer in Enum.GetNames(typeof(Antworten))
                             select new SelectListItem { Text = answer, Value = answer, Selected = false })

    /// Gets or sets a value indicating whether active.
    public bool Active { get; set; }

    /// Gets or sets a value indicating whether ja.
    /// nach bisherigen Antworten.
    public bool Ja { get; set; }

    /// Gets or sets a value indicating whether handlungsbedarf.
    public bool Handlungsbedarf { get; set; }

    /// Gets or sets a value indicating whether beratungsbedarf.
    public bool Beratungsbedarf { get; set; }

    /// Gets or sets a value indicating whether unzutreffend.
    public bool Unzutreffend { get; set; }

    /// Gets or sets a value indicating whether unbeantwortet.
    public bool Unbeantwortet { get; set; }

    /// Gets the answer values.
    public IEnumerable AnswerValues
        get { return this.answerValues; }
The enum Antworten contains 6 values, numbered from 0 to 5. The List and IEnumerable are generic, the codeplex text editor eats the generics. It's "List of Antworten" and "IEnumerable of Antworten".
Closed Jan 23, 2013 at 6:04 AM by JamesNK
I have fixed this bug. You could successfully serialize that class by removing the [Serializable] attribute in the meantime.


CarstenSchuette wrote Jan 23, 2013 at 7:23 AM

Hi James, unfortunately, removing the [Serializable] attribute was a workaround does not help.

CarstenSchuette wrote Jan 23, 2013 at 7:25 AM

Ahh okay... sorry, removing [Serializable] helps, but causes other side effects in the software. So thank you for the fix, I think I will have to use a custom build of Json.NET until you've released 4.5.12.

wrote Feb 22, 2013 at 2:47 AM

wrote May 16, 2013 at 1:36 PM

markkesty wrote Aug 13, 2013 at 9:01 PM

Removing the [Serializable] attribute is not an option if the data must be serialized to Xml. I have a program which serializes its data to xml and saves it on the disk. The same program transmits the deserialized object over a WebApi and so must be reserialized back to Json.