我有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
class 创建 class 并填充它并序列化它而不是A
,但我认为有更优雅的方法来做到这一点。
你能建议我怎么做吗?