【发布时间】:2016-09-14 20:28:53
【问题描述】:
我有 3 个函数,它们是相同类型的分支。
public interface k
{
void CreateOrUpdate(IList<TagInfoForRecommender> tagList, IndexType indexType);
void CreateOrUpdate(IList<ArtifactInfoForRecommender> artifactList, IndexType indexType);
void CreateOrUpdate(IList<UserInfoForRecommender> userList, IndexType indexType);
}
我想创建一种泛型类型,其中继承接口的实现类可以编写函数的重载方法。
我试过了
public interface k
{
void CreateOrUpdate<T>(IList<T> tagList, IndexType indexType)
where T : BaseInfoForRecommender;
}
但它只能在实现的类中创建一个方法。
我想在
中创建重载public class d : K
{
CreateOrUpdate<TagInfoForRecommender>(IList<TagInfoForRecommender> tagList, IndexType indexType)
{
//impelement sth
}
CreateOrUpdate<TagInfoForRecommender>(IList<TagInfoForRecommender> tagList, IndexType indexType)
{
//impelement sth
}
}
【问题讨论】:
-
目前尚不清楚重载会出现在哪里。您能否给出一个完整(但最少)的示例来说明您希望能够做什么?
-
请写一些代码(你想要的变种,即使它没有编译)。
-
我已经编辑了帖子。我想要实现接口中的重载
-
这不是泛型的用途。无论
T如何,他们都应该提供一个相同的模板。否则,您将不得不在方法内部的typeof(T)上进行分支,这几乎不会使设计变得更好。这是某种存储库模式吗?因为 EF/NHibernate 通常具有不关心确切类型的通用创建/更新方法。
标签: c# generics overloading