使用 List的最佳方式 并公开 Collection

发布于 2024-07-07 22:17:19 字数 729 浏览 6 评论 0原文

我必须实现一个公开值列表(整数、自定义类等)的 Web 服务。 我的工作解决方案返回 List,根据 FxCop,最好返回 CollectionReadOnlyCollection

如果我选择返回 ReadOnlyCollection,Web 服务会显示如下错误:

要实现 XML 序列化,从 ICollection 继承的类型必须在其继承层次结构的所有级别上实现 Add(System.Int32)System.Collections.ObjectModel.ReadOnlyCollection 1 [[System.Int32,mscorlib,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089]] 未实现 Add(System.Int32)

您最喜欢在内部使用 List 并公开 Collection 的方式是什么? (使用 C#,最好仅使用框架 2.0)

I must implement a web service which expose a list of values (integers, custom classes etc).
My working solution returns a List<T>, and according to FxCop it is better to return a Collection<T> or ReadOnlyCollection<T>.

If I choose to return a ReadOnlyCollection<T>, the web service shows an error like:

To be XML serializable, types which inherit from ICollection must have an implementation of Add(System.Int32) at all levels of their inheritance hierarchy.
System.Collections.ObjectModel.ReadOnlyCollection 1 [[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] does not implement Add(System.Int32).

What is your favorite way to use internally a List<T> and expose a Collection<T> ? (using C#, and preferably framework 2.0 only)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟燃烟灭 2024-07-14 22:17:19

列表或集合在这种情况下都很好。

就原始问题而言,您可以将 List包装起来: 在集合中 非常简单:

List<Foo> list = new List<Foo>();
// ...
Collection<Foo> col = new Collection<Foo>(list);

这是一个真正的包装器; 将一个项目添加到包装器 (col),然后它就会添加到列表中。 这可能会有点令人困惑,因为许多此类构造函数使用参数来进行初始填充,但不链接到原始列表。 集合是一个例外;-p

由于您处于 Web 服务边界,因此 FxCop 的建议不适用。 这很有用(与 Eric Lippert 最近的blog),以防止调用者破坏被调用者的内存 - 但在 Web 服务分布式场景中,这根本不适用。 事实上,由于 Web 服务在某些通用场景中存在一些有据可查的问题,因此简单的数组在 Web 服务边界上可以说非常有用且实用。 在 Eric 博客的上下文中 - 在这种情况下,不存在调用者/被调用者问题,因为两者之间存在强制屏障。

就 WSDL/mex 而言,我怀疑所有 3 个(列表/集合/数组)都将成为一个元素块 - 因此您可以选择最方便的一个。

List<T> or Collection<T> are fine in this case.

In terms of the original question, you can wrap a List<T> in a Collection<T> very simply:

List<Foo> list = new List<Foo>();
// ...
Collection<Foo> col = new Collection<Foo>(list);

This is a true wrapper; add an item to the wrapper (col), and it gets added to the list. This can be slightly confusing, because many such constructors use the argument to do the initial population, but don't link to the original list. Collection<T> is an exception ;-p

Since you are on a web-service boundary, that recommendation from FxCop doesn't apply. That is useful (inline with Eric Lippert's recent blog) to prevent a caller stomping over the callee's memory - but in a web-service distributed scenario that simply doesn't apply. In fact, since web-services have a few well documented issues with certain generic scenarios, a simple array is arguably very usable and pragmatic at a web-service boundary. In the context of Eric's blog - in this case, there is no question of the caller/callee issue, since there is an enforced barrier between the two.

In terms of WSDL/mex, I suspect all 3 (list/collection/array) will just become a block of elements - so you may a well go with whichever is most convenient.

瑾夏年华 2024-07-14 22:17:19

我通常返回 IList; 来自 WCF Web 服务:FxCop 对此很满意。
不确定这是否适用于 ASMX Web 服务。

I usually return IList<T> from a WCF web service: FxCop is happy enough with this.
Not sure if this works with ASMX web services.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文