IAM試圖創建一個通用存儲庫爲我查找對象,並寫:摘要泛型類的泛型約束
public interface ILookupRepository<T> :
IDisposable,
ICanGetAll<T>,
ICanGetById<T, int>
where T: LookUpObject
{
}
ICan...
的接口定義粒狀操作庫,這樣我可以使用組合來定義行爲
我想限制這個接口只爲我查找對象,所以我用的是where T: LookUpObject
約束
這是抽象類:
public abstract class LookUpObject<TObject, TKeyType> : IKeyedEntity<TKeyType>
where TObject : class
where TKeyType : struct
{
private TKeyType id;
private string description;
private bool valid;
public TKeyType Id
{
get { return id; }
set { id = value; }
}
public string Description
{
get { return description; }
set { description= value; }
}
public bool Valid
{
get { return valid; }
set { valid= value; }
}
protected LookUpObject()
{
}
}
,但我不知道如何在我的回購類中定義的約束:
我嘗試
public interface ILookupRepository<T> :
IDisposable,
ICanGetAll<T>,
ICanGetById<T, int>
where T: LookUpObject<T1, TkeyType> where T1: class
where TkeyType: Type
,但它不承認T1
和TkeyType
是可能的事情?
編輯
溶液@Grax與TkeyType的不是int關鍵
public interface ILookupRepository<T, TkeyType> :
IDisposable,
ICanGetAll<T>,
ICanGetById<T, TkeyType>
where T : LookUpObject<T, TkeyType>
where TkeyType : struct
這可能只是'T1'和'TKeyType'需要在'ILookupRepository'上。不完全確定。 – Magus