我有自定義控件,並且我有接口這個控件暴露給它的用戶。使用通用類實現非通用接口
public interface ILookupDataProvider
{
void GetDataAsync(string parameters, Action<IEnumerable<object>> onSuccess, Action<Exception> onError);
}
需要實現它,像這樣:
public class LookupDataProvider<T> : ILookupDataProvider
{
public void GetDataAsync(string parameters, Action<IEnumerable<T>> onSuccess, Action<Exception> onError)
{
var query = new EntityQuery<T>();
this.entityManager.ExecuteQueryAsync(
query,
op =>
{
if (op.CompletedSuccessfully)
{
onSuccess(op.Results);
}
else if (op.HasError)
{
onError(op.Error);
}
});
}
}
所以,我怎麼告訴這個通用方法是真正實現的接口?
我認爲這是正確的解決方案 - 除了爲什麼要改變類不再使用泛型參數? –
@KirkWoll看到我的編輯 – JoshBerke
但我的觀點是,您可以在類中保留類型參數,但仍然使用顯式接口實現,就像您在原始答案中所做的一樣。我並不是說它本身更好,但它涉及更少地改變OP的原始代碼,這似乎是最好的。 –