IList实现分为三类:只读,固定大小,可变大小

时间:2021-01-14 19:32:50

What does this mean? I am returning a IList<T> from my business layer and then adding items from the UI, but the app is complaining that it's a fixed-size list. How can I overcome this problem?

这是什么意思?我从业务层返回IList ,然后从UI添加项目,但应用程序抱怨它是一个固定大小的列表。我怎样才能克服这个问题?

3 个解决方案

#1


Could you just create a new list? IE:

你能创建一个新列表吗? IE:

List<myObject> foo = new List<myObject>(someClass.getReadOnlyList(...))

If you've got to get the list back to the business logic, check to make sure there's not some other add() functionality (insert, add, append, prepend, etc). Some classes don't allow you to directly modify their internal collections as they prefer to do some sanity checking first, or perform some other type of working with the new data that might not should be exposed to the consumer.

如果您必须将列表返回到业务逻辑,请检查以确保没有其他add()功能(插入,添加,追加,前置等)。有些类不允许您直接修改其内部集合,因为他们首先要进行一些健全性检查,或者执行其他类型的处理可能不应该向消费者公开的新数据。

#2


As far as I know you can't tell programmatically whether or not you'll be able to add to a writable list. On the other hand, the most common example is probably Array - so you could always try:

据我所知,你不能以编程方式告诉你是否能够添加到可写列表。另一方面,最常见的例子可能是数组 - 所以你总是可以尝试:

if (list is Array)
{
    // Copy to another list or whatever
}

Low-tech as it seems, I'd just indicate in the business layer the properties of the list you'll be returning - whether it'll be writable or not, etc. Or just assume that it won't be writable, and create a new list in the UI anyway.

看似低技术,我只是在业务层中指出你将要返回的列表的属性 - 它是否可写,等等。或者只是假设它不可写,并且无论如何,在UI中创建一个新列表。

#3


If you have control over your business logic (e.g. it's not code-gen'd) then the easiest solution will likely be to make sure that you're using an IList<T> implementation that's not fixed size (such as a List<T>).

如果您可以控制业务逻辑(例如,它不是代码生成),那么最简单的解决方案可能是确保您使用的IList 实现不是固定大小(例如List )。

#1


Could you just create a new list? IE:

你能创建一个新列表吗? IE:

List<myObject> foo = new List<myObject>(someClass.getReadOnlyList(...))

If you've got to get the list back to the business logic, check to make sure there's not some other add() functionality (insert, add, append, prepend, etc). Some classes don't allow you to directly modify their internal collections as they prefer to do some sanity checking first, or perform some other type of working with the new data that might not should be exposed to the consumer.

如果您必须将列表返回到业务逻辑,请检查以确保没有其他add()功能(插入,添加,追加,前置等)。有些类不允许您直接修改其内部集合,因为他们首先要进行一些健全性检查,或者执行其他类型的处理可能不应该向消费者公开的新数据。

#2


As far as I know you can't tell programmatically whether or not you'll be able to add to a writable list. On the other hand, the most common example is probably Array - so you could always try:

据我所知,你不能以编程方式告诉你是否能够添加到可写列表。另一方面,最常见的例子可能是数组 - 所以你总是可以尝试:

if (list is Array)
{
    // Copy to another list or whatever
}

Low-tech as it seems, I'd just indicate in the business layer the properties of the list you'll be returning - whether it'll be writable or not, etc. Or just assume that it won't be writable, and create a new list in the UI anyway.

看似低技术,我只是在业务层中指出你将要返回的列表的属性 - 它是否可写,等等。或者只是假设它不可写,并且无论如何,在UI中创建一个新列表。

#3


If you have control over your business logic (e.g. it's not code-gen'd) then the easiest solution will likely be to make sure that you're using an IList<T> implementation that's not fixed size (such as a List<T>).

如果您可以控制业务逻辑(例如,它不是代码生成),那么最简单的解决方案可能是确保您使用的IList 实现不是固定大小(例如List )。