如何在 C# 中创建泛型方法重载?
How to create a generic method overload in C#?
我有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);
}
我想创建一个泛型类型,其中继承接口的实现 class 可以编写函数的重载方法。
我试过了
public interface k
{
void CreateOrUpdate<T>(IList<T> tagList, IndexType indexType)
where T : BaseInfoForRecommender;
}
但它只能在已实现的方法中创建一个方法class。
我想在
中创建重载
public class d : K
{
CreateOrUpdate<TagInfoForRecommender>(IList<TagInfoForRecommender> tagList, IndexType indexType)
{
//impelement sth
}
CreateOrUpdate<TagInfoForRecommender>(IList<TagInfoForRecommender> tagList, IndexType indexType)
{
//impelement sth
}
}
你不能那样做。
唯一可以接近您要实现的目标(如果我很了解您的问题)的方法是进行一些类型检查:
public interface IAbstraction
{
void CreateOrUpdate<T>(IList<T> tagList, IndexType indexType)
where T : BaseInfoForRecommender;
}
实施:
public class Concrete : IAbstraction
{
void CreateOrUpdate<T>(IList<T> tagList, IndexType indexType)
where T : BaseInfoForRecommender
{
var dict = new Dictionary<Type, Action<IList<object>, IndexType>()
{
{ typeof(TagInfoForRecommender),
(tagList, indexType) => CreateOrUpdateTagInfoForRecommender(list.Cast<TagInfoForRecommender>(), index) },
{ typeof(ArtifactInfoForRecommender),
(tagList, indexType) => CreateOrUpdateArtifactInfoForRecommender(list.Cast<ArtifactInfoForRecommender>(), index) },
{ typeof(UserInfoForRecommender),
(tagList, indexType) => CreateOrUpdateUserInfoForRecommender(list.Cast<UserInfoForRecommender>(), index) },
};
dict[typeof(T)](tagList.Cast<object>(), indexType);
}
private CreateOrUpdateTagInfoForRecommender(IList<TagInfoForRecommender> tagList, IndexType indexType)
{
}
private CreateOrUpdateArtifactInfoForRecommender(IList<ArtifactInfoForRecommender> tagList, IndexType indexType)
{
}
private CreateOrUpdateUserInfoForRecommender(IList<UserInfoForRecommender> tagList, IndexType indexType)
{
}
}
我想你可以写得更好,因为我没有尝试我的代码(你应该有一些错误)。但是你有主要的想法。
您可以使用通用接口
public interface K<T> where T : BaseInfoForRecommender{
void CreateOrUpdate(IList<T> list, IndexType indexType);
}
然后为每个类型多次实现接口
public class d : K<TagInfoForRecommender>,
K<ArtifactInfoForRecommender>,
K<UserInfoForRecommender> {
public void CreateOrUpdate(IList<TagInfoForRecommender> list, IndexType indexType) {...}
public void CreateOrUpdate(IList<ArtifactInfoForRecommender> list, IndexType indexType) {...}
public void CreateOrUpdate(IList<UserInfoForRecommender> list, IndexType indexType) {...}
}
我有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);
}
我想创建一个泛型类型,其中继承接口的实现 class 可以编写函数的重载方法。
我试过了
public interface k
{
void CreateOrUpdate<T>(IList<T> tagList, IndexType indexType)
where T : BaseInfoForRecommender;
}
但它只能在已实现的方法中创建一个方法class。
我想在
中创建重载public class d : K
{
CreateOrUpdate<TagInfoForRecommender>(IList<TagInfoForRecommender> tagList, IndexType indexType)
{
//impelement sth
}
CreateOrUpdate<TagInfoForRecommender>(IList<TagInfoForRecommender> tagList, IndexType indexType)
{
//impelement sth
}
}
你不能那样做。
唯一可以接近您要实现的目标(如果我很了解您的问题)的方法是进行一些类型检查:
public interface IAbstraction
{
void CreateOrUpdate<T>(IList<T> tagList, IndexType indexType)
where T : BaseInfoForRecommender;
}
实施:
public class Concrete : IAbstraction
{
void CreateOrUpdate<T>(IList<T> tagList, IndexType indexType)
where T : BaseInfoForRecommender
{
var dict = new Dictionary<Type, Action<IList<object>, IndexType>()
{
{ typeof(TagInfoForRecommender),
(tagList, indexType) => CreateOrUpdateTagInfoForRecommender(list.Cast<TagInfoForRecommender>(), index) },
{ typeof(ArtifactInfoForRecommender),
(tagList, indexType) => CreateOrUpdateArtifactInfoForRecommender(list.Cast<ArtifactInfoForRecommender>(), index) },
{ typeof(UserInfoForRecommender),
(tagList, indexType) => CreateOrUpdateUserInfoForRecommender(list.Cast<UserInfoForRecommender>(), index) },
};
dict[typeof(T)](tagList.Cast<object>(), indexType);
}
private CreateOrUpdateTagInfoForRecommender(IList<TagInfoForRecommender> tagList, IndexType indexType)
{
}
private CreateOrUpdateArtifactInfoForRecommender(IList<ArtifactInfoForRecommender> tagList, IndexType indexType)
{
}
private CreateOrUpdateUserInfoForRecommender(IList<UserInfoForRecommender> tagList, IndexType indexType)
{
}
}
我想你可以写得更好,因为我没有尝试我的代码(你应该有一些错误)。但是你有主要的想法。
您可以使用通用接口
public interface K<T> where T : BaseInfoForRecommender{
void CreateOrUpdate(IList<T> list, IndexType indexType);
}
然后为每个类型多次实现接口
public class d : K<TagInfoForRecommender>,
K<ArtifactInfoForRecommender>,
K<UserInfoForRecommender> {
public void CreateOrUpdate(IList<TagInfoForRecommender> list, IndexType indexType) {...}
public void CreateOrUpdate(IList<ArtifactInfoForRecommender> list, IndexType indexType) {...}
public void CreateOrUpdate(IList<UserInfoForRecommender> list, IndexType indexType) {...}
}