How to serialize an IList<T>?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 09:20:06

If it really is a List<T> but you want to protect against change and have it still work, then the most performant solution will be to attempt to cast it to a list, and if that fails then create a new list from its content, e.g.

var data = mapper.QueryForList<T>(statement, parameters);
var list = data as List<T> ?? new List<T>(data);

However, you mention that you can't return an interface because it's a web service. This may have been true with ASMX and the XmlSerializer class, but if you build the web service using WCF and use the DataContractSerializer then it will happily serialize collection interfaces (both as inputs to and outputs from the service). That type of change may be somewhat larger than you're looking for though!

Why should you serialize IList :) Just use it as a source for your own collection and serialize it:

var data = mapper.QueryForList<T>(statement, parameters);
var yourList = new List<T>(data);
//Serialize yourList here ))

I don't think you need a c). It should be pretty safe to use solution a).

This solution depends on the public api of IBatis.net. Good api's don't change their public api unless it's really the only solution to a mission critical problem

Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!