实现ICollection的 CopyTo,自定义实体泛型类问题

wangkuang5 wangkuang5 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

//自定义泛型集合 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);
}

}

...全文

相关推荐

评论 9
收藏 已收藏
分享
返回顶部
wangkuang5
C# 111,129 成员 642,538 内容