У меня есть class A
, полученное из System.Data.Objects.DataClasses.EntityObject
.
Когда я пытаюсь сериализовать, используя
var a = new A();
DataContractJsonSerializer serializer = new DataContractJsonSerializer(a.GetType());
serializer.WriteObject(Response.OutputStream, a);
Я получаю ошибку
TestController+A._Id' is not marked with OptionalFieldAttribute, thus indicating that it must be serialized. However, 'TestController+A' derives from a class marked with DataContractAttribute and an IsReference setting of 'True'. It is not possible to have required data members on IsReference classes. Either decorate 'TestController+A._Id' with OptionalFieldAttribute, or disable the IsReference setting on the appropriate parent class.
Даже если я украслю поле OptionalFieldAttribute
, я получу
The type 'TestController+A' cannot be serialized to JSON because its IsReference setting is 'True'. The JSON format does not support references because there is no standardized format for representing references. To enable serialization, disable the IsReference setting on the type or an appropriate parent class of the type.
Я не могу изменить EntityObject
класс. Я думал создать класс A_Bag
в точности как класс A
, заполнить его и сериализовать вместо A
, но я думаю, что есть более элегантный способ сделать это.
Можете ли вы подсказать, как я могу это сделать?