2011-05-05 43 views
1

我有一個類型:使用集合來存儲仿製藥,其類型實現共同的接口

ExcelSheet<T> 

我有實現接口的一些類型:

IAddress 
public class Instructor : IAddress 
public class Student : IAddress 

我想這樣做的以下。

.... 
ExcelSheet<Instructor> instructorSheet = GetSheet<Instructor>(); 
ExcelSheet<Student> student = GetSheet<Student>(); 

List<ExcelSheet<IAddress>> sheetsWithAddress = new List<ExcelSheet<IAddress>> 
    { 
     instructorSheet, 
     student 
    } 

正如所寫,這是不可能的。我正在使用c#4.0 有沒有辦法讓這個工作?
這是一個壞主意嗎?

回答

1

如果您將ExcelSheet中的T約束爲IAddress,它將起作用。

class ExcelSheet<T> where T : IAddress 
+0

-1:這是不正確的。他的例子仍然不起作用,因爲「ExcelSheet 」與「ExcelSheet 」不同。協方差是Matias指出的正確方法。 – 2011-05-05 14:40:24