110,534
社区成员
发帖
与我相关
我的任务
分享
//自定义泛型集合 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; }
}
........
}
EMPPlayers<T>[] ary=new EMPPlayers<T>[Count];
//然后为每个元素new实例
Array.Copy(ary, array, 0);
//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);
}
public class EMPPlayers<T> : List<T> where T : PlayerObject
{
}