//自定义泛型集合 IList<T>--->[ICollection<T>, IEnumerable<T>, IEnumerable] , IList
public class EMPPlayers<T> : IList<T>, ICollection where T : PlayerObject
{ private readonly static string pGUID
= DateTime.UtcNow.Ticks.ToString().Trim('-') + "-" + Guid.NewGuid().ToString();
private static volatile EMPPlayers<T> instane = null;
private List<T> mlist = null;
protected EMPPlayers()
{
mlist = new List<T>();
} public static EMPPlayers<T> GetPlayerInstane()
{
System.Threading.Mutex mutex = new System.Threading.Mutex();
mutex.WaitOne();
if (instane == null) //双检查
{
lock (pGUID)
{
if (instane == null)
{
instane = new EMPPlayers<T>();
}
//else { //}
}
}
mutex.Close();
return instane; } #region IList<T> 成员 public int IndexOf(T item)
{
return mlist.IndexOf(item);
} public void Insert(int index, T item)
{
mlist.Insert(index, item);
} public void RemoveAt(int index)
{
mlist.RemoveAt(index);
} public T this[int index]
{ get
{
if (index >= 0 && mlist.Count > index)
return mlist[index];
return default(T);
}
set
{
mlist[index] = value;
}
} #endregion #region ICollection<T> 成员 public void Add(T item)
{
mlist.Add(item);
} public void Clear()
{
mlist.Clear();
} public bool Contains(T item)
{
return mlist.Contains(item);
} public void CopyTo(T[] array, int arrayIndex)
{
mlist.CopyTo(array, arrayIndex);
} public int Count
{
get { return mlist.Count; }
} public bool IsReadOnly
{
get
{
return false;
}
} public bool Remove(T item)
{
return mlist.Remove(item);
} #endregion #region IEnumerable<T> 成员 public IEnumerator<T> GetEnumerator()
{
//return mlist.ToArray();
foreach (T obj in mlist)
{
yield return obj;
}
} #endregion #region IEnumerable 成员 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
//System.Collections.ArrayList enumObj = new System.Collections.ArrayList();
//foreach (T item in mlist)
//{
// enumObj.Add(T);
//}
//enumObj.GetEnumerator();
return GetEnumerator();
} #endregion
//#region IList 成员 //public int Add(object value)
//{
// throw new Exception("The method or operation is not implemented.");
//} //public bool Contains(object value)
//{
// throw new Exception("The method or operation is not implemented.");
//} //public int IndexOf(object value)
//{
// throw new Exception("The method or operation is not implemented.");
//} //public void Insert(int index, object value)
//{
// throw new Exception("The method or operation is not implemented.");
//} //public bool IsFixedSize
//{
// get { throw new Exception("The method or operation is not implemented."); }
//} //public void Remove(object value)
//{
// throw new Exception("The method or operation is not implemented.");
//} //object IList.this[int index]
//{
// get
// {
// throw new Exception("The method or operation is not implemented.");
// }
// set
// {
// throw new Exception("The method or operation is not implemented.");
// }
//} //#endregion #region ICollection 成员 public void CopyTo(Array array, int index)
{
//T[] aT = new T[] { (T)array.Clone() };
//T[] xT = array.Clone() as T[];
//if (xT == null) {
// throw new ArgumentNullException();
//}
//CopyTo(xT, 0);
//mlist.CopyTo(array, 0);
//array.CopyTo(mlist.ToArray(), 0);
Array.Copy(mlist.ToArray(), array, 0);
} public bool IsSynchronized
{
get { return mlist.ToArray().IsSynchronized; }
} public object SyncRoot
{
get { return mlist.ToArray ().SyncRoot; }
} #endregion public void AddRange(IEnumerable<T> collection)
{
mlist.AddRange(collection);
}
public int LastIndexOf(T item) {
return mlist.LastIndexOf(item);
}
}
public class PlayerObject : Object
{
private string playerID; public string PlayerID
{
get { return playerID; }
set { playerID = value; }
}
private string groupID; public string GroupID
{
get { return groupID; }
set { groupID = value; }
}
private string deviceID; public string DeviceID
{
get { return deviceID; }
set { deviceID = value; }
}........
}在其他方法中
ArrayList array_players=new ArrayList (EMPPlayers<PlayerObject>.GetPlayerInstane())
会调用CopyTo
但返回的数组包含的都是空对象。求教怎么获得有效的拷贝
以及实现IList接口
{
}因为List<T>已经实现了IList<T>, IEnumerable<T>, ICollection等,我们可以什么都不用写。
其次,我没看出你实现Singleton的动机。如果不要也罢,那么连class EMPPlayers<T>都不用写了,直接用List<T>不是更简单?
//CopyTo要这样实现吧? public void CopyTo(Array array, int index)
{
//T[] aT = new T[] { (T)array.Clone() };
//T[] xT = array.Clone() as T[];
//if (xT == null) {
// throw new ArgumentNullException();
//}
//CopyTo(xT, 0);
//mlist.CopyTo(array, 0);
//array.CopyTo(mlist.ToArray(), 0); //Array.Copy(mlist.ToArray(), array, 0);
EMPPlayers<T>[] ary=new EMPPlayers<T>[Count];
Array.Copy(ary, array, 0);
}
说的有道理,
实现Singleton的动机是我在Winform程序中实现MVC模式。在程序全局只要维护一个EMPPlayers<PlayerObject>范型集合
为了方便的EMPPlayers<PlayerObject>.GetPlayerInstane()[i] 索引当前列表的对象PlayerObject
EMPPlayers<PlayerObject>集合的数据在主窗口的listview显示
EMPPlayers<T>[] ary=new EMPPlayers<T>[Count];
//然后为每个元素new实例
Array.Copy(ary, array, 0);