gpt4 book ai didi

c#泛型类的具体覆盖

转载 作者:行者123 更新时间:2023-12-04 14:48:18 25 4
gpt4 key购买 nike

这是我正在使用的通用类:

 public interface IRepository<T> where T : EntityObject
{
RepositoryInstructionResult Add(T item);
RepositoryInstructionResult Update(T item);
RepositoryInstructionResult Delete(T item);
}
public class Repository<T> : IRepository<T> where T : EntityObject
{

RepositoryInstructionResult Add(T item)
{ //implementation}
RepositoryInstructionResult Update(T item);
{ //implementation}
RepositoryInstructionResult Delete(T item);
{ //implementation}
}

现在我希望在 t :特定类型时偶尔改变方法的行为。
像下面这样的事情可能吗?
这个特殊的尝试给出了一个错误(错误 5:'Repository' 的部分声明必须以相同的顺序具有相同的类型参数名称)。
public class Repository<Bar> //where Bar : EntityObject
{
RepositoryInstructionResult Add(Bar item)
{ //different implementation to Repository<T>.Add() }
//other methods inherit from Repository<T>
}

最佳答案

public class BarRepository : Repository<Bar>
{
RepositoryInstructionResult Add(Bar item)
{ //different implementation to Repository<T>.Add() }
//other methods inherit from Repository<T>
}

关于c#泛型类的具体覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11034275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com