开发者

How to serialise an instance of a class containing private members?

开发者 https://www.devze.com 2023-03-02 18:42 出处:网络
I want to serialise an instance of a class to IsolatedStorage.I have tried SharpSerialiser ,but i开发者_如何学编程t can serialise only public properties.But my class has private members(with no proper

I want to serialise an instance of a class to IsolatedStorage.I have tried SharpSerialiser ,but i开发者_如何学编程t can serialise only public properties.But my class has private members(with no properties) ,static members etc.In DataContractSerializer also i think we can serialise only public members.So is there any way to serialise it?

Thanks and Regards vaysage


Silverlight has a harsher reflection security model, and does not support the usual tricks used to bypass this. AFAIK, you are limited to public members.

You could of course make the object self-serializing, perhaps via a custom interface and code-generation into a partial class (to avoid manual implementation).

However, IMO a better approach here is to create a secondary DTO class model; that is mutable and which has public properties with get and set. Most serializers will be happy with that, and you can always provide a conversion operator between your DTO model and your primary object model.


My preference is for custom binary serialization, which ensures that you only serialize what you actually need, is the fastest serialization method, and enables you to serialize private members if you really need to. Kevin Marshall has a great serialization comparison post that covers the options and gives performance figures, too: http://blogs.claritycon.com/kevinmarshall/2010/11/03/wp7-serialization-comparison/

0

精彩评论

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

关注公众号