ServiceStack.Text:强制序列化未使用DataMember属性修饰的属性

问题描述:

我有以下课程(简体).

I have the following class (simplified).

[DataContract]
public class ServiceResponse
{
    public int Sequence { get; set; }

    [DataMember]
    public ServiceResponseStatus Status { get; set; }
}

我正在使用ServiceStack序列化对象以进行日志记录,然后让DataContractSerializer完成其余工作.正如预期的那样,ServiceStack不会序列化Sequence,因为它没有用DataMember属性修饰.

I'm using ServiceStack to serialize objects for logging purposes, and letting DataContractSerializer do the rest. ServiceStack, as expected, will not serialize Sequence as it is not decorated with a DataMember attribute.

是否有一种方法可以强制ServiceStack序列化此属性?

Is there a way to force ServiceStack to serialize this property?

Marc Gravell对这个类似问题的答案不具有DataMember属性的DataContract序列化似乎表明一种不同的方法,但我想我会问.

Marc Gravell's answer to this similar question DataContract Serialization without DataMember Attribute seems to suggest a different approach, but I thought I'd ask.

使用DataContract/DataMember属性装饰类是专门用于选择要序列化的属性.如果要序列化所有属性,请删除所有属性.

Decorating your classes with DataContract/DataMember attributes is specifically used for opting in which properties you want serialized. If you want all properties serialized remove all the attributes.

请参阅此答案,以忽略Servicestack序列化程序中的属性的其他方式.

See this answer for other ways to ignore properties in Servicestack serializers.