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

wangkuang5 2009-04-22 11:08:31

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

}

...全文
370 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangkuang5 2009-04-22
  • 打赏
  • 举报
回复

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接口
itcrazyman 2009-04-22
  • 打赏
  • 举报
回复
mark up 帮顶,问题不太明白
ericzhangbo1982111 2009-04-22
  • 打赏
  • 举报
回复
什么问题?
Garnett_KG 2009-04-22
  • 打赏
  • 举报
回复

EMPPlayers<T>[] ary=new EMPPlayers<T>[Count];
//然后为每个元素new实例
Array.Copy(ary, array, 0);
wangkuang5 2009-04-22
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 gomoku 的回复:]
为什么要自己实现ICollection呢,代码多了,以后需要维护的也就多了。

要是我,我或许会这样做:

C# code
public class EMPPlayers<T> : List<T> where T : PlayerObject
{
}



因为List <T>已经实现了IList <T>, IEnumerable <T>, ICollection等,我们可以什么都不用写。


其次,我没看出你实现Singleton的动机。如果不要也罢,那么连class EMPPlayers <T>都不用写了,直接用List <T>不是更简单?
[/Quote]

说的有道理,
实现Singleton的动机是我在Winform程序中实现MVC模式。在程序全局只要维护一个EMPPlayers<PlayerObject>范型集合
为了方便的EMPPlayers<PlayerObject>.GetPlayerInstane()[i] 索引当前列表的对象PlayerObject
EMPPlayers<PlayerObject>集合的数据在主窗口的listview显示
Garnett_KG 2009-04-22
  • 打赏
  • 举报
回复

//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);



}


wartim 2009-04-22
  • 打赏
  • 举报
回复
太长了,没心思看啊,感觉有点设计过度了
gomoku 2009-04-22
  • 打赏
  • 举报
回复
为什么要自己实现ICollection呢,代码多了,以后需要维护的也就多了。

要是我,我或许会这样做:

public class EMPPlayers<T> : List<T> where T : PlayerObject
{
}

因为List<T>已经实现了IList<T>, IEnumerable<T>, ICollection等,我们可以什么都不用写。


其次,我没看出你实现Singleton的动机。如果不要也罢,那么连class EMPPlayers<T>都不用写了,直接用List<T>不是更简单?
wangkuang5 2009-04-22
  • 打赏
  • 举报
回复
怎么没人回答呢?郁闷!

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧