IEnumerable<System.Windows.Documents.FixedPage> page;
枚举我怎样才能添加页面(例如:d:\ newfile.txt)呢?我试过Add
,Append
,Concat
等但没有为我工作。
IEnumerable<System.Windows.Documents.FixedPage> page;
枚举我怎样才能添加页面(例如:d:\ newfile.txt)呢?我试过Add
,Append
,Concat
等但没有为我工作。
是,能够
,能够级联在一起的序列(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 });
}
}
}
IEnumerable<T>
不包含修改集合的方法。
您将需要实现ICollection<T>
或IList<T>
,因为它们包含添加和删除功能。
IEnumerable<T>
是一个只读接口。您应该使用IList<T>
,它提供了添加和删除项目的方法。
如果你打算使用更具体的接口,我会使用最高级的,你可以拿走,这将是'ICollection
IEnumerable
是不可变的。你不能添加项目,你不能删除项目。
System.Collections.Generic
的类将返回此接口,以便您可以遍历集合中包含的项目。
从MSDN
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.
参见here为MSDN参考。
您不能将元素添加到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时,可能成功获取集合,但所有修改操作都会抛出NotSupportedException
。这对于只读集合是如此。在这种情况下,构造函数的方法是唯一的选择。
ICollection<T>
接口实现了IEnumerable<T>
,所以你可以使用pageCollection
无论你正在使用page
。
尝试
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);
如果您有什么样的原始类型了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可能不会期望集合被修改那种方式。
您最好将stringList2转换为ICollection
你已经尝试过'Concat'?怎么样?您可以将单个页面包装在一个集合中,例如'新[] {singlePage}'。那么你可以使用'Enumerable.Concat':'var pages = page.Concat(new [] {singlePage});' –
非常感谢你 – Sudha