关于继承虚拟类的问题

taia 2005-04-27 10:56:32
//以下是完整的代码
using System;
using System.Collections;

class Test
{
public static void Main(){
DateCollection s=new DateCollection();
s.Add(DateTime.Now);
}
}
public class DateCollection : CollectionBase
{
public int Add(DateTime value)
{
return (List.Add(value));
}

public void Remove( DateTime value )
{
List.Remove( value );
}

public bool Contains(DateTime value)
{
return (List.Contains(value));
}

//问题出在下面这个方法中
public override void RemoveAt(int index)
{
base.RemoveAt(index);
}

public void Insert (int index,DateTime value)
{
List.Insert(index,value);
}

public DateTime this[int index]
{
get
{
return ((DateTime)List[index]);
}
set
{
List[index]=value;
}
}
}

//======================================

编译提示出错
"jc.cs(43,25): error CS0506: “DateCollection.RemoveAt(int)” : 无法重写继承成员“System.Collections.CollectionBase.RemoveAt(int)”,因为它未标记为 virtual、abstract 或 override"

大家可以翻一下SDK文档,System.Collections.CollectionBase.RemoveAt(int)这个方法的签名是
[C#]
public virtual void RemoveAt(
int index
);

明白着是虚方法啊,并且还是PUBLIC类型的
我的程序为什么会这样呢
...全文
152 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
possible_Y 2005-04-28
  • 打赏
  • 举报
回复
我用reflector看了一下System.Collections.CollectionBase.RemoveAt(int)这个方法

public void RemoveAt(int index)
{
if ((index < 0) || (index >= this.InnerList.Count))
{
throw new ArgumentOutOfRangeException(Environment.GetResourceString("ArgumentOutOfRange_Index"));
}
object obj1 = this.InnerList[index];
this.OnValidate(obj1);
this.OnRemove(index, obj1);
this.InnerList.RemoveAt(index);
this.OnRemoveComplete(index, obj1);
}

并不是virtual的(这个方法应该是实现的IList接口的方法)
而OnRemove和OnRemoveComplete都是protected virtual的(标准的模版方法模式)
Richardhu 2005-04-28
  • 打赏
  • 举报
回复
下面是我做的,和继承的不同,但实现的功能一样。
public class SchedulerAfcCollection: ICollection,IEnumerable
{
private ArrayList aArrayList = new ArrayList();

public SchedulerAfcCollection()
{}
/// <summary>
/// the int index
/// </summary>
public SchedulerAfc this[int intIndex]
{
get
{
return (SchedulerAfc)this.aArrayList[intIndex];
}
}

/// <summary>
/// the stirng index
/// </summary>
public SchedulerAfc this[string TaskID]
{
get
{
foreach(object o in this.aArrayList)
{
if(((SchedulerAfc)o).Task.TaskInfo.ID == TaskID)
return (SchedulerAfc)o;
}
return null;
}
}

public void CopyTo(Array a,int intIndex)
{
this.aArrayList.CopyTo(a,intIndex);
}

/// <summary>
/// the item count
/// </summary>
public int Count
{
get
{
return this.aArrayList.Count;
}
}

public object SyncRoot
{
get{return this;}
}
public bool IsSynchronized
{
get{return false;}
}

public IEnumerator GetEnumerator()
{
return aArrayList.GetEnumerator();
}
/// <summary>
/// add new object with instrance
/// </summary>
/// <param name="aDataGridAfcItemDataSource"></param>
public void Add(SchedulerAfc scheuler)
{
aArrayList.Add(scheuler);
}

/// <summary>
/// remove task from array by task id.
/// </summary>
/// <param name="TaskID">task id</param>
public void Remove(string TaskID)
{
foreach(object o in this.aArrayList)
{
if(((SchedulerAfc)o).Task.TaskInfo.ID == TaskID)
{
//dispose object
((SchedulerAfc)o).Task.Dispose();
((SchedulerAfc)o).Dispose();
this.aArrayList.Remove(o);
break;
}
}
}
/// <summary>
/// remove task from array by task id.
/// </summary>
/// <param name="TaskID">task id</param>
public void Remove(SchedulerAfc scheduler)
{
foreach(object o in this.aArrayList)
{
if(((SchedulerAfc)o).Task.TaskInfo.ID == scheduler.Task.TaskInfo.ID)
{
//dispose object
scheduler.Task.Dispose();
scheduler.Dispose();
this.aArrayList.Remove(o);
break;
}
}
}
}
sskset 2005-04-28
  • 打赏
  • 举报
回复
楼上说的有道理
liduke 2005-04-28
  • 打赏
  • 举报
回复
public override void Remove( DateTime value )
{
List.Remove( value );
}
因为标为virtual所以你重写时应该加上override
taia 2005-04-28
  • 打赏
  • 举报
回复
呵呵,SDK的一个BUG,其实这个方法并不是虚方法,后来用reflector看的时候证明了我的想法.
haoco 2005-04-28
  • 打赏
  • 举报
回复
up

110,552

社区成员

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

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

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