2012-08-31 51 views
1

的名单我有一类自定义泛型泛型类

public class ID_Name<T> 
{ 
    public ID_Name(T id) 
    { 
     this.ID = id; 
    } 

    public T ID { get; set; } 

    public string Name 
    { 
     get 
     { 
      return Helper.SomeReturnValue;    
     } 
    } 
} 

所有我想要做的就是产生ID_NAME的自定义列表,我可以传递一个ID_Name.ID中添加参数。 我试过如下:

public class ID_Name_List<T> : IList<T> where T : ID_Name<T> 

但后来我得到以下错误:

The type "EProtokollStatus" cannot be used as type parameter "T" in the generic type or method "ID_Name_List<\T>". There is no boxing conversion or type parameter conversion from "EProtokollStatus" in ID_Name<\EProtokollStatus>.

我读到一些关于这个问题在这里:No boxing or type parameter conversion for generic Type parameter,但我看不到的限制,除了ID_NAME这里。 这在某种程度上可能是错误的,因为我想表达的所有内容都是“在ID_Name_List中使用与ID_Name相同的类型T”,但我该如何实现这一点?

我在这里找到了一些东西:C#: Declaring and using a list of generic classes with different types, how?但我不想为所有可能的类型创建许多不同的类。

所有我想要实现的是类似

ID_Name_List<EProtokollStatus> myList = new ID_Name_List<EProtokollStatus>(); 
myList.Add(EProtokollStatus.ValueX); 

回答

4

您当前的约束是没有意义的。您的意思是:

public class ID_Name_List<T> : IList<ID_Name<T>> 
+0

是的,这就是我的意图。非常感谢! – UNeverNo