将参数从 ArrayList 迁移到 List

发布于 2024-07-26 03:51:54 字数 774 浏览 2 评论 0原文

我有一个大型项目,最初是用 C#1.1 编写的,现在针对 C#2.0。 有几个类具有采用 ArrayList 参数的方法,我想开始将它们移动以接受 List 作为参数,但这不能一次性完成,所以我需要接受这两个参数。

我进行迁移的主要原因是 List<> 对象将被接受为参数,但我也想利用类型安全。

考虑过

1)创建重载并复制代码。 这样做的好处是允许我将旧式代码标记为已过时,但显然重复代码是不好的。

2)将参数更改为 IEnumerable : 这样做的优点是可以同时使用旧代码和新代码,但不是类型安全的,因为它会接受应该是 ListList

3 ) 重写方法以采用 List 参数,并编写一个瘦包装器,该包装器采用 ArrayList 参数并将项目复制到新的 List 并然后调用main方法。 这还有一个优点是允许我将旧样式代码标记为已过时。 它会对性能/GC 造成轻微影响。

4) 创建三个重载。 一种采用 IEnumerable 的私有方法和两种仅调用私有方法的公共包装方法(一种用于 ArrayList,一种用于 List)。 这还有一个优点是允许我将旧样式代码标记为已过时。 不知道除了三种方法之外是否还有其他缺点。

我倾向于4,但我忽略了什么吗?

I have a large project which was originally written in C#1.1 and is now targeting C#2.0. There are several classes which have methods which take an ArrayList parameter I would like to start to move these to accept List<T> as a parameter, but this can't be done all at one time, so I need to accept both parameters.

My main reason for the migration is so that List<> objects will be accepted as parameters, but I would also like to take advantage of type safety.

Have considered

1) Creating overloads and duplicating the code.
This has the advantage of allowing me to mark the old style code as obsolete, but obviously duplicating code is bad.

2) Changing the parameter to IEnumerable :
This has the advantage of working with both old and new code, but is not type-safe as it will accept List<T1> where it should be List<T2>

3) Re-writing the methods to take a List<T> parameter and writing a thin wrapper which takes an ArrayList parameter and copies the items to a new List<T> and then calls the main method. This also has the advantage of allowing me to mark the old style code as obsolete. It will have a slight performance/GC hit.

4) Creating three overloads. One private method which take IEnumerable and two public wrapper methods (one for ArrayList and one for List<T>) which just call the private method.
This also has the advantage of allowing me to mark the old style code as obsolete. Not sure whether it has any dis-advantages other than having three methods.

I am leaning towards 4, but am I overlooking anything?

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

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

发布评论

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

评论(3

烟雨凡馨 2024-08-02 03:51:54

一定要使用 #2 的变体,即使用 IEnumerable 而不是 List。 对于那些需要 IEnumerable 以外的内容的人,您的目标应该是 IList

对于那些可以迁移到 IEnumerable的方法,您可以在短期内将它们仅迁移到 IEnumerable,这样就无需维护该方法的两个副本。 然后,在大部分 ArrayList 消失后,您可以轻松完成向 IEnumerable 的迁移。

Definitely use a variant of #2, where you shoot for IEnumerable<T> rather than List<T>. For those that need more than IEnumerable<T>, your goal should be IList<T>.

For those methods that can migrate to IEnumerable<T>, you can move them to just IEnumerable for the short term, and in this way avoid needing to maintain two copies of the method. Then, after most of your ArrayLists are gone you can complete the move to IEnumerable<T> with very little effort.

掩饰不了的爱 2024-08-02 03:51:54

我将创建一套完整的 IEnumerable (或者,如果您需要操作而不是枚举功能,则可能是 IList)参数方法,并将这些方法设为您的默认方法做所有的工作。 为了与您的 ArrayList 合约兼容,您可以将原始 ArrayList 方法自身转换为 IEnumerable 并调用 IEnumerable 方法。

基本上将工作转移到类型更安全的选项,并为旧合同创建一个外观。

I would create a complete suite of IEnumerable<t> (or perhaps IList<t> if you need manipulation rather than enumeration capabilities) parameter methods and make those your default methods that do all of the work. For compatibility with your ArrayList contract, you could then have your original ArrayList methods translate themselves into IEnumerable<t> and call the IEnumerable methods.

Basically shift the work to your more type-safe option and create a facade for your old contract.

如若梦似彩虹 2024-08-02 03:51:54

我想补充一下 Joel 的出色答案,您可以在过渡阶段使用现有方法作为“真实”方法,并简单地从 IEnumerable 重载调用来避免复制开销IEnumerable 一。

这将是一个快速而简单的更改,几乎不会引入二进制不兼容性。

您可以使用 [Obsolete("please move to the generic implementation", false)] 标记 IEnumerable 方法(抑制其他重载的警告),然后稳步移动到“正确”版本。

完成此操作后,您可以将实际方法转换为依赖于类型安全泛型的方法并删除链接。

I would add to Joel's excellent answer that you can avoid the copy overhead by, in your transition phase, using the existing method as you 'real' method and simply call from the IEnumerable<T> overload to the IEnumerable one.

This would be a quick and easy change, with almos no binary incompatibility introduced.

You can mark the IEnumerable method with [Obsolete("please move to the generic implementation", false)] (suppressing the warning on the other overload) then steadily move to the 'correct' version.

Once that is done you can convert the real method to one relying on the type safe generics and drop the chaining.

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