Я думаю, это может произойти, потому что вы запускаете свой сервер из IDEA или Eclipse, который проверяет, хорошо ли ваше развертывание или нет. Я согласен с ответом Ульриха, вы должны вызвать URL-адрес, используя curl или wget. И попробуйте запустить сервер не из IDE (если есть).
Массивы являются счетными, таким образом, Ваш foo
уже IEnumerable<byte>
самостоятельно. Просто используйте методы последовательности LINQ как Take()
для получения то, что Вы хотите из него (не забывайте включать Linq
пространство имен с using System.Linq;
):
byte[] foo = new byte[4096];
var bar = foo.Take(41);
при реальной необходимости в массиве от какого-либо IEnumerable<byte>
значение Вы могли бы использовать ToArray()
метод для этого. Это, кажется, не имеет место здесь.
Я не думаю, что C# поддерживает семантику Диапазона. Вы могли записать дополнительный метод хотя, как:
public static IEnumerator<Byte> Range(this byte[] array, int start, int end);
, Но как другие сказали, не должны ли Вы устанавливать индекс запуска тогда Take
, все, в чем Вы нуждаетесь.
Можно использовать, Берут дополнительный метод
var array = new byte[] {1, 2, 3, 4};
var firstTwoItems = array.Take(2);
Вы могли использовать обертку вокруг исходного массива (который является IList), как в этой (непротестированной) части кода.
public class SubList<T> : IList<T>
{
#region Fields
private readonly int startIndex;
private readonly int endIndex;
private readonly int count;
private readonly IList<T> source;
#endregion
public SubList(IList<T> source, int startIndex, int count)
{
this.source = source;
this.startIndex = startIndex;
this.count = count;
this.endIndex = this.startIndex + this.count - 1;
}
#region IList<T> Members
public int IndexOf(T item)
{
if (item != null)
{
for (int i = this.startIndex; i <= this.endIndex; i++)
{
if (item.Equals(this.source[i]))
return i;
}
}
else
{
for (int i = this.startIndex; i <= this.endIndex; i++)
{
if (this.source[i] == null)
return i;
}
}
return -1;
}
public void Insert(int index, T item)
{
throw new NotSupportedException();
}
public void RemoveAt(int index)
{
throw new NotSupportedException();
}
public T this[int index]
{
get
{
if (index >= 0 && index < this.count)
return this.source[index + this.startIndex];
else
throw new IndexOutOfRangeException("index");
}
set
{
if (index >= 0 && index < this.count)
this.source[index + this.startIndex] = value;
else
throw new IndexOutOfRangeException("index");
}
}
#endregion
#region ICollection<T> Members
public void Add(T item)
{
throw new NotSupportedException();
}
public void Clear()
{
throw new NotSupportedException();
}
public bool Contains(T item)
{
return this.IndexOf(item) >= 0;
}
public void CopyTo(T[] array, int arrayIndex)
{
for (int i=0; i<this.count; i++)
{
array[arrayIndex + i] = this.source[i + this.startIndex];
}
}
public int Count
{
get { return this.count; }
}
public bool IsReadOnly
{
get { return true; }
}
public bool Remove(T item)
{
throw new NotSupportedException();
}
#endregion
#region IEnumerable<T> Members
public IEnumerator<T> GetEnumerator()
{
for (int i = this.startIndex; i < this.endIndex; i++)
{
yield return this.source[i];
}
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
}
static byte[] SliceMe(byte[] source, int length)
{
byte[] destfoo = new byte[length];
Array.Copy(source, 0, destfoo, 0, length);
return destfoo;
}
//
var myslice = SliceMe(sourcearray,41);
Если Вы хотите IEnumerable<byte>
, то всего
IEnumerable<byte> data = foo.Take(x);
Вы могли использовать массивы CopyTo()
метод.
Или с LINQ можно использовать Skip()
и Take()
...
byte[] arr = {1, 2, 3, 4, 5, 6, 7, 8};
var subset = arr.Skip(2).Take(2);
Вы могли использовать ArraySegment<T>
. Это очень легко, поскольку это не копирует массив:
string[] a = { "one", "two", "three", "four", "five" };
var segment = new ArraySegment<string>( a, 1, 2 );