将List 转换为object

我正在寻找一个将List转换为object[]单行程序。 这是一个class轮,所以我对foreach等解决方案不感兴趣,或者for

任何接受者?

提示:不, List.ToArray()List.ToArray()都不起作用。

编辑:为什么List.ToArray()不起作用? 因为它无法编译。

 mylist.Cast().ToArray() 

顺便说一句,这只会迭代一次,以防你想知道性能。 上)。 🙂

为什么? 好吧,因为Cast将使用延迟执行,并且在ToArray()迭代列表之前实际上不会执行任何操作。

 List.Select(x => x as object).ToArray(); 

应该返回一个object[]

如果您没有Linq(.Net 3.0),那么您可以使用List中的ConvertAll()和ToArray()方法:

 List list = new List(); object[] objects = list.ConvertAll(item => (object)item).ToArray(); 
 theList.Cast().ToArray() 

要么

 new List(theList).ToArray() 

对于LINQ之前的解决方案(仅适用于参考类型)。

 (object[])List.ToArray(); 

如果您不介意编写一个非常简短的可重用函数,ConvertAll扩展方法可能会有所帮助:

http://msdn.microsoft.com/en-us/library/73fe8cwf.aspx

编辑:

这也会奏效

 List intList = new List() { 1, 3, 4 }; object[] objectList = intList.ConvertAll(item => (object)item).ToArray(); 

在.NET 2.0上的C#(VS 2008)中,以下编译并且不使用LINQ(据我所见)作为引用类型。

  object[] oArray; List oList = new List(); oArray = oList.ToArray(); 

这不需要强制转换,因为所有引用类型都以对象为基础。

我建议创建一个ListCastAdapter,

假设您要将列表转换为列表

创建IList实现的实现,该实现从List返回项

我很可能称之为ListCastAdapter类

祝你有美好的一天

实施(未测试):

注意 :建议将列表设置为ReadOnly,因为现在用户可以在原始列表中插入层次结构中的对象。

注意 :未实现CopyTo(),您可以为数组创建相同的想法。

 using System.Collections; using System.Collections.Generic; namespace UDF.MyDataLayer { internal class ListCastAdapter : IList where T : class where S : class { private List adaptee; public ListCastAdapter(List adaptee ) { this.adaptee = adaptee; } #region Implementation of IEnumerable public class EnumeratorCastAdapter : IEnumerator { private IEnumerator adaptee; public EnumeratorCastAdapter(IEnumerator adaptee) { this.adaptee = adaptee; } #region Implementation of IDisposable ///  /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. ///  /// 2 public void Dispose() { adaptee.Dispose(); } #endregion #region Implementation of IEnumerator ///  /// Advances the enumerator to the next element of the collection. ///  ///  /// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection. ///  /// The collection was modified after the enumerator was created. 2 public bool MoveNext() { return adaptee.MoveNext(); } ///  /// Sets the enumerator to its initial position, which is before the first element in the collection. ///  /// The collection was modified after the enumerator was created. 2 public void Reset() { adaptee.Reset(); } ///  /// Gets the element in the collection at the current position of the enumerator. ///  ///  /// The element in the collection at the current position of the enumerator. ///  public T Current { get { // needs to check if it is an Object or Value Type return adaptee.Current as T; } } ///  /// Gets the current element in the collection. ///  ///  /// The current element in the collection. ///  /// The enumerator is positioned before the first element of the collection or after the last element.2 object IEnumerator.Current { get { return Current; } } #endregion } ///  /// Returns an enumerator that iterates through the collection. ///  ///  /// A  that can be used to iterate through the collection. ///  /// 1 public IEnumerator GetEnumerator() { return new EnumeratorCastAdapter(adaptee.GetEnumerator()); } ///  /// Returns an enumerator that iterates through a collection. ///  ///  /// An  object that can be used to iterate through the collection. ///  /// 2 IEnumerator IEnumerable.GetEnumerator() { return adaptee.GetEnumerator(); } #endregion #region Implementation of ICollection ///  /// Adds an item to the . ///  /// The object to add to the .The  is read-only. public void Add(T item) { adaptee.Add(item as S); } ///  /// Removes all items from the . ///  /// The  is read-only.  public void Clear() { adaptee.Clear(); } ///  /// Determines whether the  contains a specific value. ///  ///  /// true if  is found in the ; otherwise, false. ///  /// The object to locate in the . public bool Contains(T item) { return adaptee.Contains(item as S); } ///  /// Copies the elements of the  to an , starting at a particular  index. ///  /// The one-dimensional  that is the destination of the elements copied from . The  must have zero-based indexing.The zero-based index in  at which copying begins. is null. is less than 0. is multidimensional.-or-The number of elements in the source  is greater than the available space from  to the end of the destination .-or-Type  cannot be cast automatically to the type of the destination . public void CopyTo(T[] array, int arrayIndex) { throw new System.NotImplementedException("Not Needed by Me, implement ArrayCastAdapter if needed"); } ///  /// Removes the first occurrence of a specific object from the . ///  ///  /// true if  was successfully removed from the ; otherwise, false. This method also returns false if  is not found in the original . ///  /// The object to remove from the .The  is read-only. public bool Remove(T item) { adaptee.Remove(item as S); } ///  /// Gets the number of elements contained in the . ///  ///  /// The number of elements contained in the . ///  public int Count { get { return adaptee.Count; } } ///  /// Gets a value indicating whether the  is read-only. ///  ///  /// true if the  is read-only; otherwise, false. ///  public bool IsReadOnly { get { return true; // change, to live on the edge } } #endregion #region Implementation of IList ///  /// Determines the index of a specific item in the . ///  ///  /// The index of  if found in the list; otherwise, -1. ///  /// The object to locate in the . public int IndexOf(T item) { return adaptee.IndexOf(item as S); } ///  /// Inserts an item to the  at the specified index. ///  /// The zero-based index at which  should be inserted.The object to insert into the . is not a valid index in the .The  is read-only. public void Insert(int index, T item) { adaptee.Insert(index, item as S); } ///  /// Removes the  item at the specified index. ///  /// The zero-based index of the item to remove. is not a valid index in the .The  is read-only. public void RemoveAt(int index) { adaptee.RemoveAt(index); } ///  /// Gets or sets the element at the specified index. ///  ///  /// The element at the specified index. ///  /// The zero-based index of the element to get or set. is not a valid index in the .The property is set and the  is read-only. public T this[int index] { get { return adaptee[index] as T; } set { adaptee[index] = value as S; } } #endregion } }