问题比较复杂,先看看一个图先:
本来我的程序包含Column和他的集合ColumnCollection,可以通过名称检索不同的列。
现在我要重构这段代码,我发现我的程序中包含很多通过名称检索数据的集合类,于是我定义了一个INamedObject接口,用来处理所有有名称的数据。

public interface INamedObject
{

string Name
{ get;}
} 并且定义了一个只读的集合类接口,用来检索有名称的数据。

public interface INamedCollection<T> : IEnumerable<T> where T : INamedObject
{

int Count
{ get; }
bool ContainsName(string pName);

T this[int pIndex]
{ get;}

T this[string pName]
{ get;}
} 当然了,有接口,就有一个基础实现:NamedCollection
public class NamedCollection<T>

: Collection<T>, INamedCollection<T> where T : INamedObject
{
private Dictionary<string, T> _nameIndex;

public NamedCollection()
{
_nameIndex = new Dictionary<string, T>();
}

protected override void InsertItem(int index, T item)
{
base.InsertItem(index, item);
_nameIndex.Add(item.Name, item);
}

protected override void RemoveItem(int index)
{
string removeItemName = this[index].Name;
base.RemoveItem(index);
_nameIndex.Remove(removeItemName);
}

protected override void SetItem(int index, T item)
{
throw new NotSupportedException();
}

protected override void ClearItems()
{
base.Clear();
_nameIndex.Clear();
}

public bool ContainsName(string pName)
{
return _nameIndex.ContainsKey(pName);
}

public T this[string pName]
{

get
{ return _nameIndex[pName]; }
}
} 关于Column和Collection我也提取成了接口的形式,定义如下:

public interface IColumn : INamedObject
{

System.Data.DbType DbType
{ get;}
}

public interface IColumnCollection : INamedCollection<IColumn>
{ }
Column实现了IColumn接口,非常的简单,就不罗嗦了。

public class Column : IColumn
{

public Column(string pName, System.Data.DbType pDbType)
{
}
private string _name;

public string Name
{

get
{ return _name; }
}
private System.Data.DbType _dbType;

public System.Data.DbType DbType
{

get
{ return _dbType; }
}
}
好,终于可以将正题了,我现在想实现让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为什么这样设计
此问题在已经有贴基本解决。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构