开发者

XML Serialization - handling special cases

开发者 https://www.devze.com 2023-01-07 01:14 出处:网络
I need to XML serialize some classes, which in some cases do not adhere to the rules and guidelines, that is neede开发者_如何学JAVAd in order to make built-in serialization work. This includes propert

I need to XML serialize some classes, which in some cases do not adhere to the rules and guidelines, that is neede开发者_如何学JAVAd in order to make built-in serialization work. This includes properties that are interface types and properties without setters.

I know how to implement IXmlSerializable, but it could be a lot of work; since most of the properties will need no special handling. Is there any way for me to only write code to handle these "special properties" and have the standard XML serializer serialize the properties that it knows how to serialize ?

Also, suggestions for open source libraries or similar, that can do a better job at serializing the objects to XML without too much custom code, is welcomed.


There is a tool called Automapper This tool maps two types to each other. For your problem you could build a standard conform type and map the not standard one to this one. Most of the properties will be mapped with the automapper. Special ones you could map with your own methods.

First you have to setup your mappings:

Mapper.CreateMap<ITestSequence, TestSequence>();
Mapper.CreateMap<ITestBlock, TestBlock>();

And then map your opjects:

Mapper.Map(srcTb, dstTb);

which will set all common properties. The rest you could do by yourself.

0

精彩评论

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