【发布时间】:2019-07-21 00:23:52
【问题描述】:
我想定义一个有多个(特殊和正常)房间的房子,每个房间都有一组(特殊和正常)东西。
我开始为我的 ThingCollection(和派生类)使用泛型,但是当我想定义我的房间类型时,我的泛型类型定义开始出现错误。
有谁知道定义我的接口/类的正确方法,这样我就不会收到此错误消息?
代码:
namespace City.Street.House
{
// Thing(s)
public interface IThing{ }
public interface ISpecialThing : IThing { }
// Collection(s)
public interface ThingCollection<TThing> where TThing : IThing { }
public interface SpecialThingCollection<TThing> : ThingCollection<TThing> where TThing : ISpecialThing { }
// Room(s) // Error On TThing in both rows below:
public interface Room<TThingCollection> where TThingCollection : ThingCollection<TThing> { }
public interface SpecialRoom<TThingCollection> : Room<TThingCollection> where TThingCollection : SpecialThingCollection<TThing> { }
// House(s)
public interface House { }
}
错误信息:
CS0246:找不到类型或命名空间名称“TThing”(您是否缺少 using 指令或程序集引用?)
【问题讨论】:
-
请避免添加代码或错误消息的屏幕截图 - 它们通常只会使问题更难阅读。相反,将它们作为文本包含在内。由于您已经提供了正确的代码,因此我已从您的帖子中删除了图像。欲了解更多信息,请阅读"Why not upload images of code on SO when asking a question?" 接受的答案。
标签: c# oop generics types interface