C# 如何序列化 IList<T>?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/464525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 04:26:42  来源:igfitidea点击:

How to serialize an IList<T>?

c#.netweb-services.net-3.5ibatis.net

提问by Hinek

I've got an OR mapper (iBatis.Net) that returns an IList.

我有一个返回 IList 的 OR 映射器 (iBatis.Net)。

// IList<T> QueryForList<T>(string statementName, object parameterObject);
var data = mapper.QueryForList<Something>(statement, parameters);

I'm trying to use it in an webservice an want to return the data 1:1. Of course I can't return IList in a WebMethod, because it is an interface and therefore not serializable.

我正在尝试在网络服务中使用它并希望以 1:1 的比例返回数据。当然,我不能在 WebMethod 中返回 IList,因为它是一个接口,因此不可序列化。

I found that the mapper is really returning an List. But I'm afraid to cast it to List because of course the mappers inner workings could change in future versions (and it just feels dirty).

我发现映射器真的返回了一个列表。但是我害怕将它转换为 List 因为当然映射器的内部工作在未来的版本中可能会发生变化(而且感觉很脏)。

So should I ...

那我应该...

a) return new List<Something>(data);

b) return (List<Something>)data;

c) // your solution here

Thanks a lot!

非常感谢!

采纳答案by Greg Beech

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.

如果它确实是一个List<T>但您想防止更改并使其仍然有效,那么最高效的解决方案是尝试将其转换为列表,如果失败,则从其内容创建一个新列表,例如

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 XmlSerializerclass, but if you build the web service using WCF and use the DataContractSerializerthen 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!

但是,您提到不能返回接口,因为它是 Web 服务。这对于 ASMX 和XmlSerializer类来说可能是正确的,但是如果您使用 WCF 构建 Web 服务并使用 ,DataContractSerializer那么它将很高兴地序列化集合接口(作为服务的输入和输出)。不过,这种变化可能比您正在寻找的要大一些!

回答by Norbert B.

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

我不认为你需要交流)。使用解决方案 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

该解决方案依赖于IBatis.net的公共api。好的 api 不会改变他们的公共 api,除非它真的是关键任务问题的唯一解决方案

Hope this helps.

希望这可以帮助。

回答by ProfyTroll

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

为什么要序列化 ​​IList :) 只需将其用作您自己的集合的源并将其序列化:

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