我有自定义控件,我有此控件向其用户公开的接口(interface)。
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);
}
});
}
}
那么,我怎么知道这个泛型方法真的是接口(interface)的实现呢?
请您参考如下方法:
如果您可以将类更改为通用方法,您可以这样做:
public class LookupDataProvider : ILookupDataProvider
{
public void GetDataAsync<T>(string parameters, Action<IEnumerable<T>> onSuccess, Action<Exception> onError)
{
}
void ILookupDataProvider.GetDataAsync(string parameters, Action<IEnumerable<object>> onSuccess, Action<Exception> onError)
{
this.GetDataAsync<Object>(parameters, onSuccess, onError);
}
}
编辑 关于柯克的评论,您需要在方法上移动类型参数。虽然你也可以把它留在类里面,但这会带来有趣的事情。例如运行此代码:
class Program
{
static void Main(string[] args)
{
var b = new Foo<Guid>();
b.Bar();
Console.ReadLine();
}
}
public class Foo<T>
{
public void Bar<T>()
{
Console.WriteLine("Bar<T>() : " +typeof(T).Name);
}
public void Bar()
{
Console.WriteLine("Bar() : " + typeof(T).Name);
this.Bar<string>();
}
}
这实际上是一个警告(我很惊讶它不被认为是一个异常(exception))
