[英]c# generic interface where parameter is generic
I have an interface我有一个界面
public interface IOfflineBackedRepo<TSummary,TDetail>
{
Task SyncAsync();
}
this has different concrete implementations.这有不同的具体实现。 I am trying to develop a service lets developers register different implementations of IOfflineBackedRepo
interface and separately call SyncAsync
at a later point in time.我正在尝试开发一项服务,让开发人员可以注册IOfflineBackedRepo
接口的不同实现,并在稍后的时间点分别调用SyncAsync
。 The new interface should be similar to the below, except this is not a valid syntax.新界面应类似于以下内容,只是这不是有效的语法。
public interface ISyncManager<T> where T : IOfflineBackedRepo<TSummary, TDetail>
{
void Register(T repo);
Task SyncNowAsync(); // this loops through and calls SyncAsync on the repo
}
How to fix the syntax issue?如何解决语法问题?
ISyncManager
does not have anywhere mentioning TSummary,TDetail
in it's scope ISyncManager
在它的范围内没有任何地方提到ISyncManager
TSummary,TDetail
TSummary
and TDetail
is defined and exist only around IOfflineBackedRepo
. TSummary
和TDetail
被定义并且只存在于IOfflineBackedRepo
周围。 When you try to use it anywhere else you must define a type you will use in substitute for them.当您尝试在其他任何地方使用它时,您必须定义一个类型来替代它们。 It could be actual type or a type that you would get from generic argument but you must define it somewhere around ISyncManager
它可以是实际类型或您将从泛型参数中获得的类型,但您必须在ISyncManager
周围的某个地方定义它
At the most basic ways,以最基本的方式,
public interface ISyncManager<T,U,V> where T : IOfflineBackedRepo<U,V>
{
void Register(T repo);
Task SyncNowAsync(); // this loops through and calls SyncAsync on the repo
}
For the ease of use I think you should just separate interface为了便于使用,我认为你应该单独的界面
public interface IOfflineBackedRepo
{
Task SyncAsync(); // this function is not typed specific right?
}
public interface IOfflineBackedRepo<TSummary,TDetail> : IOfflineBackedRepo
{
// other definition about type specific
}
public interface ISyncManager<T> where T : IOfflineBackedRepo
{
void Register(T repo);
Task SyncNowAsync(); // this loops through and calls SyncAsync on the repo
}
And I guess ISyncManager
is actually just a collection.我猜ISyncManager
实际上只是一个集合。 I think it would be better if you could just make extension method for collection我想如果你能做一个收集的扩展方法会更好
public static class SyncManagerExt
{
// Register is just Add to collection
public static Task SyncNowAsync(this ICollection<T> items) where T : IOfflineBackedRepo
{
// loops through items and calls SyncAsync on the repo
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.