這不起作用:獲取與接口泛型類型對象只設置方法的類型
public interface IServerFuncs
{
Table<T> getTable<T>() where T : MasterClass;
//*cut* other stuff here
}
public class DefaultFuncs<T> : IServerFuncs where T : MasterClass
{
Table<T> table;
public DefaultFuncs(Table<T> table)
{
this.table = table;
}
public Table<T> getTable()
{
return table;
}
}
它最高審計機關DefaultFuncs<T>' does not implement interface member 'IServerFuncs.getTable<T>()'
但我不能做到這一點無論:
public Table<T> getTable<T>() where T:MasterClass
{
return table;
}
現在是Error: Cannot implicitly convert type 'MySQLCache.Table<T>
。我猜噸與DefaultFuncs<T>
方法的衝突,所以我嘗試:
public Table<T2> getTable<T2>() where T2:MasterClass
{
return table;
}
但它給出了另一個錯誤:Error Cannot implicitly convert type 'Table<T>' to 'Table<T2>'
我需要得到這個無需添加泛型類型,以IServerFuncs
(IServerFuncs<T>
)工作..有任何想法嗎?
基礎'getTable()'不應該是通用的。另外,公共成員名稱應該是UpperCamelCase。 – SLaks
由於'getTable'允許客戶端選擇'T'類型,即使只有一種可能的類型,也沒有一種解決方法。如果你不能使'IServerFuncs'成爲通用的,那麼你唯一的選擇是拋出一個異常,如果'T2!= T'或者添加一個非通用接口到'表'並返回。 –
Lee