how to convert IList to IList<T> and vice verse?
-
2006年11月28日 15:32Thanks
全部回复
-
2006年11月28日 16:13
Since IList doesn't derive from IList<T> (directly or indirectly) (for any T) you'll have to write a wrapper.
using
System;using
System.Collections;using
System.Collections.Generic;using
System.Text;namespace
ListWrapper{
class EnumeratorWrapper<T> : IEnumerator<T>{
IEnumerator e; public EnumeratorWrapper(IEnumerator e){
this.e = e;}
#region
IEnumerator<T> Members public T Current{
get { return (T)e.Current; }}
#endregion
#region
IDisposable Members public void Dispose(){
if (e is IDisposable)((
IDisposable)e).Dispose();}
#endregion
#region
IEnumerator Members object IEnumerator.Current{
get { return e.Current; }}
public bool MoveNext(){
return e.MoveNext();}
public void Reset(){
e.Reset();
}
#endregion
}
public class ListWrapper<T> : IList<T>{
IList list; public ListWrapper(IList list){
this.list = list;}
#region
IList<T> Members public int IndexOf(T item){
return list.IndexOf(item);}
public void Insert(int index, T item){
list.Insert(index, item);
}
public void RemoveAt(int index){
list.RemoveAt(index);
}
public T this[int index]{
get{
return (T)list[index];}
set{
list[index] =
value;}
}
#endregion
#region
ICollection<T> Members public void Add(T item){
list.Add(item);
}
public void Clear(){
list.Clear();
}
public bool Contains(T item){
return list.Contains(item);}
public void CopyTo(T[] array, int arrayIndex){
list.CopyTo(array, arrayIndex);
}
public int Count{
get { return list.Count; }}
public bool IsReadOnly{
get { return list.IsReadOnly; }}
public bool Remove(T item){
bool contains = list.Contains(item); if (contains)list.Remove(item);
return contains;}
#endregion
#region
IEnumerable<T> Members
public IEnumerator<T> GetEnumerator(){
return new EnumeratorWrapper<T>(list.GetEnumerator());}
#endregion
#region
IEnumerable Members IEnumerator IEnumerable.GetEnumerator(){
return list.GetEnumerator();}
#endregion
}
class Program{
static void Main(string[] args){
ArrayList blah = new ArrayList();blah.Add(1);
blah.Add(2);
blah.Add(3);
IList<int> w = new ListWrapper<int>(blah); foreach (int i in w){
Console.WriteLine(i);}
Console.ReadLine();}
}
}
-
2006年11月29日 11:29Or, somewhat shorter:
IList ilist = new ArrayList();
ilist.Add("One");
ilist.Add("Two");
ilist.Add("Three");
// Convert to IList<string>
IList<string> slist = ConvertToListOf<string>(ilist);
// Convert back to IList:
IList olist = new ArrayList(((List<string>)slist).ToArray());
...
private IList<T> ConvertToListOf<T>(IList iList)
{
IList<T> result = new List<T>();
foreach (T value in iList)
result.Add(value);
return result;
}
-
2010年6月15日 16:35
IList<Class > A = new IList<Classe >();
List<Class > B = (List<Class >)A ;
-
2010年6月16日 0:02
Let me add some explanation to point out some details on the solutions you've received so far. BigBrain's solution is a wrapper. Modifications made through that wrapper will affect the original list. Matthew's solution, while shorter, will make a copy of your list. Modifications made through the copy will not affect the original list. Keep this in mind when evaluating which way makes sense for your application. If you aren't modifying the list and it does not contain a large number of elements, the short, simple solution might be all you need. Otherwise, a wrapper is better.
-
2010年6月16日 3:36
Depending on what semantics you need, the answer could be as simple as:
// assuming IList untypedList
var typedList = untypedList.OfType<T>().ToList();
-
2010年6月25日 0:29
public class A
{private int _ID = 0;
public int ID
{
get
{
return _ID;
}
set
{
_ID= value;
}
}public A(int iD)
{
_ID = iD;
}
}
Program:
private Main()
{
IList<A > _A = new IList<A >();
_A.Add(new A(1));
_A.Add(new A(2));
_A.Add(new A(3));
List<A> _A1 = (List<A>)_A;
}

