2017-07-27 78 views
-1

當做到這一點:將接口傳遞給C#中類的一般屬性?

public class ball { 
    ITeste<IItem> Teste{ get; set; } 
} 

我得到了錯誤:

'IItem' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method

ITeste:

public interface ITeste<T> 
     where T : IItem, IEquatable<T>, new() 
{ 
    //ITeste code 
} 

的iItem:

public interface IItem : IEquatable<IItem> 
{ 
    //IItem Code 
} 

這似乎是一個簡單的問題,但我不能找到答案。任何幫助,將不勝感激。

+5

嗯,是的,你不能用'IItem'作爲'ITeste'一個類型參數,因爲它不滿足你在'ITeste'中用'T'表達的限制。這樣說 - 你不能寫'new IItem()',因爲它是一個接口... –

+0

感謝Jon的回答,我沒有看到。 =) –

回答

0

從約束ITeste

刪除新的()

ITeste:

public interface ITeste<T>where T : IItem, IEquatable<T> 
{ 
    //ITeste code 
}