23

我有一个这样的枚举器

IEnumerable<System.Windows.Documents.FixedPage> page;

如何向其中添加页面(例如:D:\newfile.txt)?我已经尝试过Add,等等AppendConcat但没有什么对我有用。

4

7 回答 7

8

IEnumerable<T>不包含修改集合的方法。

您将需要实现ICollection<T>或者IList<T>因为它们包含添加和删除功能。

于 2013-04-02T09:43:35.490 回答
8

是的,有可能

可以将序列(IEnumerables)连接在一起并将连接的结果分配给新序列。(您不能更改原始顺序。)

内置Enumerable.Concat()只会连接另一个序列;但是,很容易编写一个扩展方法,让您将一个标量连接到一个序列。

以下代码演示:

using System;
using System.Collections.Generic;
using System.Linq;

namespace Demo
{
    public class Program
    {
        [STAThread]
        private static void Main()
        {
            var stringList = new List<string> {"One", "Two", "Three"};

            IEnumerable<string> originalSequence = stringList;

            var newSequence = originalSequence.Concat("Four");

            foreach (var text in newSequence)
            {
                Console.WriteLine(text); // Prints "One" "Two" "Three" "Four".
            }
        }
    }

    public static class EnumerableExt
    {
        /// <summary>Concatenates a scalar to a sequence.</summary>
        /// <typeparam name="T">The type of elements in the sequence.</typeparam>
        /// <param name="sequence">a sequence.</param>
        /// <param name="item">The scalar item to concatenate to the sequence.</param>
        /// <returns>A sequence which has the specified item appended to it.</returns>
        /// <remarks>
        /// The standard .Net IEnumerable extensions includes a Concat() operator which concatenates a sequence to another sequence.
        /// However, it does not allow you to concat a scalar to a sequence. This operator provides that ability.
        /// </remarks>

        public static IEnumerable<T> Concat<T>(this IEnumerable<T> sequence, T item)
        {
            return sequence.Concat(new[] { item });
        }
    }
}
于 2013-04-02T09:59:42.567 回答
6

如果您知道 IEnumerable 的原始类型是什么,您可以修改它...

List<string> stringList = new List<string>();
stringList.Add("One");
stringList.Add("Two");
IEnumerable<string> stringEnumerable = stringList.AsEnumerable();
List<string> stringList2 = stringEnumerable as List<string>;

if (stringList2 != null)
    stringList2.Add("Three");

foreach (var s in stringList)
    Console.WriteLine(s);

这输出:

One
Two
Three

将 foreach 语句更改为迭代stringList2,或者stringEnumerable,您将得到相同的结果。

反射可能有助于确定 IEnumerable 的真实类型。

不过,这可能不是一个好习惯……无论给您 IEnumerable 的内容,都可能不希望以这种方式修改集合。

于 2013-04-07T04:15:36.780 回答
5

IEnumerable<T>是一个只读接口。您应该使用 an IList<T>instead,它提供了添加和删除项目的方法。

于 2013-04-02T09:43:43.430 回答
1

IEnumerable是不可变的。您不能添加项目,也不能删除项目。
来自的类System.Collections.Generic返回此接口,因此您可以迭代集合中包含的项目。

来自 MSDN

Exposes the enumerator, which supports a simple iteration over a collection of a specified type.

有关 MSDN 参考,请参见此处

于 2013-04-02T09:44:12.607 回答
1

您不能向 中添加元素IEnumerable<T>,因为它不支持添加操作。您要么必须使用 的实现ICollection<T>,要么尽可能将其强制转换IEnumerable<T>为。ICollection<T>

IEnumerable<System.Windows.Documents.FixedPage> page;
....
ICollection<System.Windows.Documents.FixedPage> pageCollection 
    = (ICollection<System.Windows.Documents.FixedPage>) page

如果演员是不可能的,例如使用

ICollection<System.Windows.Documents.FixedPage> pageCollection 
   = new List<System.Windows.Documents.FixedPage>(page);

你可以这样做:

ICollection<System.Windows.Documents.FixedPage> pageCollection
    = (page as ICollection<System.Windows.Documents.FixedPage>) ??
      new List<System.Windows.Documents.FixedPage>(page);

后者几乎可以保证您拥有一个可修改的集合。但是,使用 cast 时,有可能成功获取集合,但所有修改操作都将 throw NotSupportedException。对于只读集合也是如此。在这种情况下,使用构造函数的方法是唯一的选择。

ICollection<T>接口实现IEnumerable<T>,因此您可以pageCollection在当前使用的任何地方使用page

于 2013-04-02T09:46:36.467 回答
1

尝试

IEnumerable<System.Windows.Documents.FixedPage> page = new List<System.Windows.Documents.FixedPage>(your items list here)

或者

IList<System.Windows.Documents.FixedPage> page = new List<System.Windows.Documents.FixedPage>(1);
page.Add(your item Here);
于 2013-04-02T09:47:44.593 回答