开发者

Add DataMemberAttribute "on the fly"

开发者 https://www.devze.com 2022-12-10 12:39 出处:网络
I have some types which are generated by a web service reference. I want to serialize these objects using the DataContractJsonSerializer, so I need to add DataContract and DataMember attributes. Addin

I have some types which are generated by a web service reference. I want to serialize these objects using the DataContractJsonSerializer, so I need to add DataContract and DataMember attributes. Adding DataContract is no problem using partial classes. But the properties have no DataMember attributes, so I only get empty ob开发者_如何学Gojects. Is there a way to get this to work in case one cannot modify the serialized types?


Unlike XmlSerializer, I don't think (from memory) that there is a ctor for passing in this extra metadata at runtime. Perhaps another viable option is to have a twin DTO class that is attributed, and shuffle the data into there? You can add a conversion method / operator (between the two) in the partial class. Not ideal maybe, but it'll work.

0

精彩评论

暂无评论...
验证码 换一张
取 消