zoukankan      html  css  js  c++  java
  • 为什么C#的范型不能自动实现此接口

    问题比较复杂,先看看一个图先:
     
    本来我的程序包含Column和他的集合ColumnCollection,可以通过名称检索不同的列。
    现在我要重构这段代码,我发现我的程序中包含很多通过名称检索数据的集合类,于是我定义了一个INamedObject接口,用来处理所有有名称的数据。
        public interface INamedObject 
    并且定义了一个只读的集合类接口,用来检索有名称的数据。
        public interface INamedCollection<T> : IEnumerable<T> where T : INamedObject 
    当然了,有接口,就有一个基础实现:NamedCollection
        public class NamedCollection<T>
        : Collection
    <T>, INamedCollection<T> where T : INamedObject 
    关于Column和Collection我也提取成了接口的形式,定义如下:
        public interface IColumn : INamedObject 

        
    public interface IColumnCollection : INamedCollection<IColumn> 

    Column实现了IColumn接口,非常的简单,就不罗嗦了。
        public class Column : IColumn 

    好,终于可以将正题了,我现在想实现让ColumnCollection实现IColumnCollection,代码如下:

    public class ColumnCollection : NamedCollection<Column>,IColumnCollection {}

    很可惜的是,Vs2005下的C#认为我的NamedCollection<Column>没有实现:IColumnCollection的实现:

    Error 1 'ConsoleApplication1.ColumnCollection' does not implement interface member 'ConsoleApplication1.INamedCollection<ConsoleApplication1.IColumn>.this[int]'. 'System.Collections.ObjectModel.Collection<T>.this[int]' is either static, not public, or has the wrong return type. C:\Documents and Settings\Steve\Local Settings\Application Data\Temporary Projects\ConsoleApplication1\Program.cs 83 15 ConsoleApplication1
    Error 2 'ConsoleApplication1.ColumnCollection' does not implement interface member 'ConsoleApplication1.INamedCollection<ConsoleApplication1.IColumn>.this[string]'. 'ConsoleApplication1.NamedCollection<T>.this[string]' is either static, not public, or has the wrong return type. C:\Documents and Settings\Steve\Local Settings\Application Data\Temporary Projects\ConsoleApplication1\Program.cs 83 15 ConsoleApplication1
    Error 3 'ConsoleApplication1.ColumnCollection' does not implement interface member 'System.Collections.Generic.IEnumerable<ConsoleApplication1.IColumn>.GetEnumerator()'. 'System.Collections.ObjectModel.Collection<ConsoleApplication1.Column>.GetEnumerator()' is either static, not public, or has the wrong return type. C:\Documents and Settings\Steve\Local Settings\Application Data\Temporary Projects\ConsoleApplication1\Program.cs 83 15 ConsoleApplication1


    有哪位大侠讲讲MS为什么这样设计


    此问题在已经有贴基本解决。

  • 相关阅读:
    [Error]错误 C2660: Gdiplus::GdiplusBase::operator new: 函数不带三个参数
    opengl多线程的问题
    去掉CFormView的滚动条
    DevIL库使用时图片翻转的问题
    让notepad++正确显示actionscript文件语法高亮
    [Error]world geometry is not supported by the generic scenemanager
    3d Max 9的"正在验证许可证"问题的解决
    CSizingControlBar Error C2440: “static_cast”: 无法从“UINT (__thiscall CSizingControlBarG::* )(CPoint)”转换为>>>
    MFC下的OpenGL
    酷!不用外挂,Win7资源监视器查看QQ好友IP
  • 原文地址:https://www.cnblogs.com/tansm/p/200452.html
Copyright © 2011-2022 走看看